winx-code-agent 0.2.352

Remote-first Rust MCP runtime with Streamable HTTP, durable PTYs, and guarded agent tools
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
use glob::glob;
use std::fmt::Write as FmtWrite;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::{debug, warn};

use crate::errors::{Result, WinxError};
use crate::state::bash_state::BashState;
use crate::state::persistence::{
    load_bash_state_from_path, save_bash_state_to_path, BashStateSnapshot,
};
use crate::types::ContextSave;
use crate::utils::path::{expand_user, validate_path_in_workspace};

/// Handle a call to the `ContextSave` tool
///
/// This function processes a `ContextSave` request, saves context information about a task,
/// including file contents from specified globs, to a single file.
///
/// # Arguments
///
/// * `bash_state` - Shared reference to the bash state
/// * `args` - Parameters for the `ContextSave` operation
///
/// # Returns
///
/// A Result with the path where the context file was saved, or an error
pub async fn handle_tool_call(
    bash_state: &Arc<Mutex<Option<BashState>>>,
    args: ContextSave,
) -> Result<String> {
    // Ensure bash state is initialized
    let bash_state_guard = bash_state.lock().await;

    let bash_state = bash_state_guard.as_ref().ok_or(WinxError::BashStateNotInitialized)?;

    // Process the ContextSave request
    let result = save_context(bash_state, args)?;

    if should_open_context_file(&result) {
        if let Err(e) = try_open_file(&result) {
            debug!("Failed to open the context file: {}", e);
        }
    }

    Ok(result)
}

/// Save the context information to a file
///
/// # Arguments
///
/// * `bash_state` - Reference to the bash state
/// * `context` - The `ContextSave` parameters
///
/// # Returns
///
/// A Result with the path where the context file was saved, or an error
fn save_context(bash_state: &BashState, mut context: ContextSave) -> Result<String> {
    normalize_context(&mut context)?;

    let (relevant_files, warnings) = collect_relevant_files(&context, &bash_state.workspace_root)?;
    let memory_dir = resolve_memory_dir()?;
    let relevant_files_data = read_files_content(&relevant_files, 10_000)?;
    // Scrub credentials before they're written to the on-disk memory file (it is
    // re-read into context on resume, so a leaked secret would persist).
    let memory_data =
        crate::utils::redact::redact(&format_memory(&context, &relevant_files_data)).into_owned();
    let safe_id = sanitize_filename(&context.id);

    let (memory_file_path, state_file_path) = context_paths_from_dir(&memory_dir, &safe_id);
    if let Some(response) = write_memory_file(&memory_file_path, &memory_data, &context)? {
        return Ok(response);
    }

    write_bash_state_file(&state_file_path, bash_state);

    Ok(context_save_response(&relevant_files, &warnings, &context, &memory_file_path))
}

fn normalize_context(context: &mut ContextSave) -> Result<()> {
    if !context.project_root_path.is_empty() {
        context.project_root_path = expand_user(&context.project_root_path);
    }

    if context.id.trim().is_empty() {
        return Err(WinxError::ArgumentParseError("Task ID cannot be empty".to_string()));
    }

    Ok(())
}

fn collect_relevant_files(
    context: &ContextSave,
    workspace_root: &Path,
) -> Result<(Vec<PathBuf>, Vec<String>)> {
    let mut relevant_files = Vec::new();
    let mut warnings = Vec::new();
    let mut skipped_outside = 0usize;

    for glob_pattern in &context.relevant_file_globs {
        // Expand the glob pattern if it contains a tilde
        let expanded_glob = expand_user(glob_pattern);

        // If the glob is not absolute and we have a project root, make it relative to the project root
        let final_glob =
            if !Path::new(&expanded_glob).is_absolute() && !context.project_root_path.is_empty() {
                PathBuf::from(&context.project_root_path)
                    .join(expanded_glob)
                    .to_string_lossy()
                    .to_string()
            } else {
                expanded_glob
            };

        debug!("Processing glob pattern: {}", final_glob);

        // Use the glob crate to find matching files
        let matches = glob(&final_glob).map_err(|e| {
            WinxError::ArgumentParseError(format!("Invalid glob pattern '{final_glob}': {e}"))
        })?;

        let mut found_files = false;
        for entry in matches {
            match entry {
                Ok(path) => {
                    if path.is_file() {
                        // Confine reads to the workspace. ContextSave inlines file
                        // contents into the saved memory, so without this an
                        // absolute glob (`/etc/**`, `~/.ssh/*`) or a `..`-escaping
                        // pattern would exfiltrate arbitrary files — over HTTP that
                        // is an authenticated read primitive. The other file tools
                        // already gate on this; ContextSave was the gap.
                        if validate_path_in_workspace(&path, workspace_root).is_err() {
                            skipped_outside += 1;
                            continue;
                        }
                        relevant_files.push(path);
                        found_files = true;
                        // Limit to 1000 files per glob to avoid excessive processing
                        if relevant_files.len() >= 1000 {
                            warn!("Reached limit of 1000 files for glob '{}'", final_glob);
                            break;
                        }
                    }
                }
                Err(e) => {
                    warn!("Error matching glob '{}': {}", final_glob, e);
                }
            }
        }

        if !found_files {
            warnings.push(format!("Warning: No files found for the glob: {glob_pattern}"));
        }
    }

    if skipped_outside > 0 {
        warnings.push(format!(
            "Warning: skipped {skipped_outside} file(s) that resolved outside the workspace root."
        ));
    }

    debug!("Found {} relevant files", relevant_files.len());
    Ok((relevant_files, warnings))
}

fn resolve_memory_dir() -> Result<PathBuf> {
    let app_dir = match get_app_dir_xdg() {
        Ok(dir) => dir,
        Err(e) => {
            debug!("Failed to get primary app directory: {:?}", e);
            // Try using temporary directory directly as a last resort
            let fallback = std::env::temp_dir().join("winx-memory");
            debug!("Using fallback directory: {}", fallback.display());
            fs::create_dir_all(&fallback).map_err(|e2| WinxError::FileAccessError {
                path: fallback.clone(),
                message: format!(
                    "Failed to create fallback directory: {e2} (after previous error: {e:?})"
                ),
            })?;
            fallback
        }
    };

    let mut memory_dir = app_dir.join("memory");
    match fs::create_dir_all(&memory_dir) {
        Ok(()) => {}
        Err(e) => {
            debug!("Failed to create memory directory: {}", e);
            // If we can't create the memory subdirectory, use the app dir directly as the memory_dir
            debug!("Using app_dir directly as memory_dir due to failed subdirectory creation");
            memory_dir = app_dir;
        }
    }

    Ok(memory_dir)
}

pub(crate) fn context_paths(id: &str) -> Result<(PathBuf, PathBuf)> {
    let safe_id = sanitize_filename(id);
    let memory_dir = resolve_memory_dir()?;
    Ok(context_paths_from_dir(&memory_dir, &safe_id))
}

fn context_paths_from_dir(memory_dir: &Path, safe_id: &str) -> (PathBuf, PathBuf) {
    (
        memory_dir.join(format!("{safe_id}.txt")),
        memory_dir.join(format!("{safe_id}_bash_state.json")),
    )
}

pub(crate) fn load_saved_context(id: &str) -> Result<Option<(String, Option<BashStateSnapshot>)>> {
    if id.is_empty() {
        return Ok(None);
    }

    let (memory_file_path, state_file_path) = context_paths(id)?;
    if !memory_file_path.exists() {
        tracing::warn!(
            task_id = %id,
            path = %memory_file_path.display(),
            "task_id_to_resume requested but no saved context found; starting fresh"
        );
        return Ok(None);
    }

    let memory_data =
        fs::read_to_string(&memory_file_path).map_err(|e| WinxError::FileAccessError {
            path: memory_file_path.clone(),
            message: format!("Failed to read saved context: {e}"),
        })?;
    // Cap the injected context so a large saved memory can't blow the Initialize
    // response past the model's window. Keep the head (project root + plan).
    let memory_data = truncate_memory_to_tokens(memory_data, memory_token_budget());
    let state = load_bash_state_from_path(&state_file_path).map_err(|e| {
        WinxError::SerializationError(format!("Failed to load saved bash state: {e}"))
    })?;

    Ok(Some((memory_data, state)))
}

/// Saved memory is treated as non-code prose; cap it like wcgw's
/// `noncoding_max_tokens` so resuming a task never floods the context window.
const MEMORY_MAX_TOKENS: usize = 8_000;

/// Memory token budget, overridable via `WINX_NONCODING_TOKEN_BUDGET` (saved
/// memory is prose, so it shares the non-coding budget with `ReadFiles`).
fn memory_token_budget() -> usize {
    crate::utils::encoder::budget_from_env("WINX_NONCODING_TOKEN_BUDGET", MEMORY_MAX_TOKENS)
}

/// Floor on each file's per-file token slice, so even a glob of many files still
/// shows a usable head of each one rather than a useless few tokens.
const MIN_PER_FILE_SAVE_TOKENS: usize = 500;

/// Byte ceiling for reading a single context file off disk. `cap_file_to_tokens`
/// trims the result to a few KB of tokens anyway, so there's no point pulling more
/// than this into memory — and a glob that matches a 50 MB `.log` would otherwise
/// read the whole thing (blocking + GBs of churn) just to drop almost all of it.
const MAX_CONTEXT_READ_BYTES: u64 = 2 * 1024 * 1024;

/// Read a file's text for context saving, bounded to `MAX_CONTEXT_READ_BYTES`.
/// Small files go through `read_to_string` unchanged (exact binary detection via
/// `InvalidData`); oversized files read only the capped head, keeping the valid
/// UTF-8 prefix when the byte cap lands mid-codepoint, and still report a
/// genuinely-binary file (invalid from byte 0) as `InvalidData`.
fn read_text_for_context(path: &Path) -> std::io::Result<String> {
    let len = fs::metadata(path)?.len();
    if len <= MAX_CONTEXT_READ_BYTES {
        return fs::read_to_string(path);
    }
    let mut buf = Vec::new();
    File::open(path)?.take(MAX_CONTEXT_READ_BYTES).read_to_end(&mut buf)?;
    match String::from_utf8(buf) {
        Ok(s) => Ok(s),
        Err(e) => {
            let valid = e.utf8_error().valid_up_to();
            if valid == 0 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "stream did not contain valid UTF-8",
                ));
            }
            // The byte cap split a multibyte char; the prefix up to `valid` is
            // valid UTF-8 by construction, so from_utf8_lossy inserts nothing.
            let bytes = e.into_bytes();
            Ok(String::from_utf8_lossy(&bytes[..valid]).into_owned())
        }
    }
}

/// Keep the first `max_tokens` of a file's content and note what was dropped.
/// Used so a single large file in a `ContextSave` glob doesn't crowd out the rest.
fn cap_file_to_tokens(content: &str, max_tokens: usize) -> String {
    if crate::utils::encoder::definitely_fits_token_budget(content, max_tokens) {
        return content.to_string();
    }
    match crate::utils::encoder::encode_ids(content) {
        Some(ids) if ids.len() > max_tokens => {
            let omitted = ids.len() - max_tokens;
            let head = crate::utils::encoder::decode_ids(&ids[..max_tokens])
                .unwrap_or_else(|| content.chars().take(max_tokens.saturating_mul(4)).collect());
            format!("{head}\n[... {omitted} tokens truncated for context save ...]")
        }
        _ => content.to_string(),
    }
}

/// Truncate saved memory to `max_tokens`, keeping the head (where the project
/// root and the plan/status live) and appending a marker. Falls back to the
/// untouched text if the tokenizer is unavailable.
fn truncate_memory_to_tokens(text: String, max_tokens: usize) -> String {
    if crate::utils::encoder::definitely_fits_token_budget(&text, max_tokens) {
        return text;
    }
    let Some(ids) = crate::utils::encoder::encode_ids(&text) else {
        return text;
    };
    if ids.len() <= max_tokens {
        return text;
    }
    let keep = max_tokens.saturating_sub(8);
    match crate::utils::encoder::decode_ids(&ids[..keep]) {
        Some(decoded) => format!("{decoded}\n(...truncated saved context)"),
        None => text,
    }
}

/// Extract the project root recorded at the top of a saved memory file, if any.
/// Matches the `Project root path: <path>` header written by `format_memory`.
pub(crate) fn extract_project_root(memory: &str) -> Option<PathBuf> {
    memory
        .lines()
        .find_map(|line| line.strip_prefix("Project root path:").map(str::trim))
        .filter(|root| !root.is_empty())
        .map(PathBuf::from)
}

fn write_memory_file(
    memory_file_path: &Path,
    memory_data: &str,
    context: &ContextSave,
) -> Result<Option<String>> {
    match File::create(memory_file_path) {
        Ok(mut file) => {
            if let Err(e) = file.write_all(memory_data.as_bytes()) {
                warn!("Failed to write memory data: {}", e);
                return Ok(Some(save_to_temp_file(memory_data, context)?));
            }
        }
        Err(e) => {
            warn!("Failed to create memory file: {}", e);
            return Ok(Some(save_to_temp_file(memory_data, context)?));
        }
    }

    Ok(None)
}

fn write_bash_state_file(state_file_path: &Path, bash_state: &BashState) {
    // Try to create and write state file, but don't fail if it doesn't work
    if let Err(e) = save_bash_state_to_path(state_file_path, &bash_state.snapshot()) {
        warn!("Failed to write bash state data: {}", e);
    }
}

fn context_save_response(
    relevant_files: &[PathBuf],
    warnings: &[String],
    context: &ContextSave,
    memory_file_path: &Path,
) -> String {
    let memory_file_path_str = memory_file_path.to_string_lossy().to_string();
    if !relevant_files.is_empty() || context.relevant_file_globs.is_empty() {
        if warnings.is_empty() {
            memory_file_path_str
        } else {
            format!(
                "{}\n\nContext file successfully saved at {}",
                warnings.join("\n"),
                memory_file_path_str
            )
        }
    } else {
        format!(
            "Error: No files found for the given globs. Context file successfully saved at \"{memory_file_path_str}\", but please fix the error."
        )
    }
}

/// Format the memory data for saving
///
/// # Arguments
///
/// * `context` - The `ContextSave` parameters
/// * `relevant_files_data` - The content of the relevant files
///
/// # Returns
///
/// A formatted string containing the memory data
/// Make a glob portable for resume: if it's absolute and under `project_root`,
/// store it relative to the root (recorded separately in the memory); otherwise
/// keep it verbatim. A clone or another machine then still resolves it.
fn relativize_glob(glob: &str, project_root: &str) -> String {
    if project_root.is_empty() {
        return glob.to_string();
    }
    Path::new(glob)
        .strip_prefix(project_root)
        .map_or_else(|_| glob.to_string(), |rel| rel.to_string_lossy().to_string())
}

fn format_memory(context: &ContextSave, relevant_files_data: &str) -> String {
    let mut memory_data = String::new();

    // Add project root path if provided
    if !context.project_root_path.is_empty() {
        let _ = write!(memory_data, "Project root path: {}\n\n", context.project_root_path);
    }

    // Add the description
    memory_data.push_str(&context.description);
    memory_data.push_str("\n\n");

    // Add the relevant file globs, made portable: an absolute glob under the
    // project root is stored relative to it (the root is recorded above), so the
    // saved memory resumes in a clone — or on another machine — where the
    // absolute path no longer exists. Already-relative globs are kept as-is.
    let portable_globs = context
        .relevant_file_globs
        .iter()
        .map(|glob| relativize_glob(glob, &context.project_root_path))
        .collect::<Vec<_>>();
    let _ = write!(memory_data, "Relevant file globs: {}\n\n", portable_globs.join(", "));

    // Add the content of the relevant files
    memory_data.push_str("File contents:\n\n");
    memory_data.push_str(relevant_files_data);

    memory_data
}

/// Get the application directory for storing data
///
/// This function tries multiple locations in order of preference:
/// 1. `XDG_DATA_HOME/winx` if `XDG_DATA_HOME` is set
/// 2. HOME/.local/share/winx if HOME is set
/// 3. Current directory/.winx-data as a fallback
/// 4. Temporary directory as a last resort
///
/// # Returns
///
/// A Result with the path to the app directory
fn get_app_dir_xdg() -> Result<PathBuf> {
    // Try multiple locations in order of preference
    let app_dir = get_primary_app_dir().or_else(|_| get_fallback_app_dir())?;

    debug!("Using app directory: {}", app_dir.display());
    Ok(app_dir)
}

/// Try to get the primary application directory
fn get_primary_app_dir() -> Result<PathBuf> {
    // Try XDG_DATA_HOME first
    if let Ok(xdg_path) = std::env::var("XDG_DATA_HOME") {
        let app_dir = PathBuf::from(xdg_path).join("winx");
        if let Ok(()) = fs::create_dir_all(&app_dir) {
            return Ok(app_dir);
        }
    }

    // Try HOME/.local/share next
    if let Ok(home) = std::env::var("HOME") {
        let app_dir = PathBuf::from(home).join(".local/share/winx");
        if let Ok(()) = fs::create_dir_all(&app_dir) {
            return Ok(app_dir);
        }
    }

    // No successful primary location
    Err(WinxError::FileAccessError {
        path: PathBuf::from("<primary-paths>"),
        message: "Could not create app directory in primary locations".to_string(),
    })
}

/// Get a fallback application directory when primary ones fail
fn get_fallback_app_dir() -> Result<PathBuf> {
    // Try current directory
    let current_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

    let app_dir = current_dir.join(".winx-data");
    if let Ok(()) = fs::create_dir_all(&app_dir) {
        return Ok(app_dir);
    }

    // Try temporary directory as a last resort
    let temp_dir = std::env::temp_dir().join("winx-data");
    fs::create_dir_all(&temp_dir).map_err(|e| WinxError::FileAccessError {
        path: temp_dir.clone(),
        message: format!("Failed to create app directory in any location: {e}"),
    })?;

    Ok(temp_dir)
}

/// Read the content of multiple files
///
/// # Arguments
///
/// * `file_paths` - List of paths to the files to read
/// * `max_files` - Maximum number of files to read
///
/// # Returns
///
/// A Result with the content of the files, or an error
fn read_files_content(file_paths: &[PathBuf], max_files: usize) -> Result<String> {
    let mut result = String::new();
    let mut skipped_binary = Vec::new();

    // Give each file a fair slice of the memory budget so one huge file (a 200KB
    // log) can't eat the whole window and bury the rest. Before this, files were
    // inlined whole and the global cap afterward kept only the head — usually
    // just the first file, losing every later file and each file's tail.
    let shown = file_paths.len().min(max_files).max(1);
    let per_file_tokens = (memory_token_budget() / shown).max(MIN_PER_FILE_SAVE_TOKENS);

    for (i, path) in file_paths.iter().take(max_files).enumerate() {
        // Try to read as UTF-8 text (capped, so a giant file can't OOM/block the
        // executor), skip binary files.
        match read_text_for_context(path) {
            Ok(file_content) => {
                let _ = writeln!(result, "--- File {}: {} ---", i + 1, path.display());
                result.push_str(&cap_file_to_tokens(&file_content, per_file_tokens));
                result.push_str("\n\n");
            }
            Err(e) if e.kind() == std::io::ErrorKind::InvalidData => {
                // Binary file or invalid UTF-8 - skip with note
                skipped_binary.push(path.display().to_string());
            }
            Err(e) => {
                return Err(WinxError::FileAccessError {
                    path: path.clone(),
                    message: format!("Failed to read file: {e}"),
                });
            }
        }
    }

    // Add note about skipped binary files
    if !skipped_binary.is_empty() {
        let _ = write!(
            result,
            "Note: Skipped {} binary/non-text file(s): {}\n\n",
            skipped_binary.len(),
            skipped_binary.join(", ")
        );
    }

    if file_paths.len() > max_files {
        let _ = writeln!(
            result,
            "Note: Only showing the first {} files out of {}.",
            max_files,
            file_paths.len()
        );
    }

    Ok(result)
}

/// Try to open a file with the default application
///
/// # Arguments
///
/// * `file_path` - Path to the file to open
///
/// # Returns
///
/// A Result indicating success or failure
fn try_open_file(file_path: &str) -> Result<()> {
    if std::env::consts::OS != "macos" && std::env::consts::OS != "linux" {
        // Skip on unsupported platforms
        return Ok(());
    }

    // Get the command to use based on the OS
    let cmd = if std::env::consts::OS == "macos" {
        "open"
    } else {
        // Try to find which command is available on Linux
        for cmd in &["xdg-open", "gnome-open", "kde-open"] {
            let status = std::process::Command::new("which")
                .arg(cmd)
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .status();

            if let Ok(status) = status {
                if status.success() {
                    // Found an available command, use it
                    let _ = std::process::Command::new(cmd)
                        .arg(file_path)
                        .stdout(std::process::Stdio::null())
                        .stderr(std::process::Stdio::null())
                        .spawn()
                        .map_err(|e| {
                            WinxError::CommandExecutionError(format!(
                                "Failed to spawn open command: {e}"
                            ))
                        })?;

                    // We don't wait for the command to complete
                    return Ok(());
                }
            }
        }

        // If no command is available, just return success
        return Ok(());
    };

    // Try to open the file
    let _ = std::process::Command::new(cmd)
        .arg(file_path)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .map_err(|e| {
            WinxError::CommandExecutionError(format!("Failed to spawn open command: {e}"))
        })?;

    // We don't actually need to wait for the command to complete
    // Just let it run in the background
    // (This mimics the Python implementation)

    Ok(())
}

fn should_open_context_file(file_path: &str) -> bool {
    crate::config::env_flag("WINX_OPEN_CONTEXT") && Path::new(file_path).is_file() && !cfg!(test)
}

/// Save context data to a temporary file as a last resort
fn save_to_temp_file(memory_data: &str, context: &ContextSave) -> Result<String> {
    let temp_dir = std::env::temp_dir();
    let safe_id = sanitize_filename(&context.id);
    let temp_file_path = temp_dir.join(format!("winx-{safe_id}.txt"));

    let mut file = File::create(&temp_file_path).map_err(|e| WinxError::FileAccessError {
        path: temp_file_path.clone(),
        message: format!("Failed to create temporary file: {e}"),
    })?;

    file.write_all(memory_data.as_bytes()).map_err(|e| WinxError::FileAccessError {
        path: temp_file_path.clone(),
        message: format!("Failed to write to temporary file: {e}"),
    })?;

    let path_str = temp_file_path.to_string_lossy().to_string();

    Ok(format!(
        "Context was saved to temporary file at {path_str} due to permission issues with regular locations."
    ))
}

/// Sanitize a filename to ensure it's valid on all platforms
fn sanitize_filename(input: &str) -> String {
    let invalid_chars = vec!['/', '\\', ':', '*', '?', '"', '<', '>', '|'];
    let mut result = input.to_string();

    for c in invalid_chars {
        result = result.replace(c, "_");
    }

    // Limit length to avoid issues. Slice by chars, not bytes: a multibyte
    // task id whose 45th byte lands mid-code-point would panic on `&result[0..45]`.
    if result.chars().count() > 50 {
        use rand::RngExt;
        let head: String = result.chars().take(45).collect();
        result = format!("{head}-{}", rand::rng().random_range(1000..9999));
    }

    result
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::{
        cap_file_to_tokens, read_text_for_context, relativize_glob, MAX_CONTEXT_READ_BYTES,
    };

    #[test]
    fn read_text_for_context_small_file_reads_whole() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("small.txt");
        std::fs::write(&p, "hello\nworld\n").unwrap();
        assert_eq!(read_text_for_context(&p).unwrap(), "hello\nworld\n");
    }

    #[test]
    fn read_text_for_context_binary_is_invalid_data() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("bin");
        std::fs::write(&p, [0xff, 0xfe, 0x00, 0x01]).unwrap();
        assert_eq!(read_text_for_context(&p).unwrap_err().kind(), std::io::ErrorKind::InvalidData);
    }

    #[test]
    fn read_text_for_context_caps_oversized_file() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("big.txt");
        let capped = usize::try_from(MAX_CONTEXT_READ_BYTES).unwrap_or(usize::MAX - 4096);
        std::fs::write(&p, vec![b'a'; capped + 4096]).unwrap();
        // Reads only the capped head, not the whole oversized file.
        assert_eq!(read_text_for_context(&p).unwrap().len() as u64, MAX_CONTEXT_READ_BYTES);
    }

    #[test]
    fn relativize_glob_makes_absolute_portable() {
        // Absolute glob under the root -> relative.
        assert_eq!(relativize_glob("/home/u/proj/src/*.rs", "/home/u/proj"), "src/*.rs");
        // Already relative -> untouched.
        assert_eq!(relativize_glob("src/*.rs", "/home/u/proj"), "src/*.rs");
        // Outside the root -> kept verbatim.
        assert_eq!(relativize_glob("/etc/*.conf", "/home/u/proj"), "/etc/*.conf");
        // No project root -> unchanged.
        assert_eq!(relativize_glob("/abs/x", ""), "/abs/x");
    }

    #[test]
    fn small_file_is_untouched() {
        let content = "def add(a, b):\n    return a + b\n";
        assert_eq!(cap_file_to_tokens(content, 4_000), content);
    }

    #[test]
    fn large_file_is_capped_with_a_note() {
        // ~5k tokens of distinct words, capped to 100.
        use std::fmt::Write as _;
        let mut big = String::new();
        for i in 0..5_000 {
            let _ = write!(big, "word{i} ");
        }
        let capped = cap_file_to_tokens(&big, 100);
        assert!(capped.len() < big.len(), "should shrink");
        assert!(capped.contains("truncated for context save"), "should note truncation");
        assert!(capped.starts_with("word0"), "should keep the head");
    }
}