tsk-ai 0.10.7

tsk-tsk: keeping your agents out of trouble with sandboxed coding agent automation
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
use anyhow::Result;
use std::path::Path;

/// Creates a directory at the specified path, including all parent directories.
pub async fn create_dir(path: &Path) -> Result<()> {
    tokio::fs::create_dir_all(path).await?;
    Ok(())
}

/// Recursively copies a directory from source to destination.
pub fn copy_dir<'a>(
    from: &'a Path,
    to: &'a Path,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
    Box::pin(async move {
        create_dir(to).await?;

        let mut entries = tokio::fs::read_dir(from).await?;
        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            let relative_path = path.strip_prefix(from)?;
            let dst_path = to.join(relative_path);

            // Get metadata without following symlinks to check if it's a symlink
            let metadata = tokio::fs::symlink_metadata(&path).await?;

            if metadata.is_symlink() {
                // Preserve symlinks by reading the target and creating a new symlink
                let target = tokio::fs::read_link(&path).await?;
                if let Some(parent) = dst_path.parent() {
                    create_dir(parent).await?;
                }
                // Create symlink at destination pointing to the same target
                #[cfg(unix)]
                tokio::fs::symlink(&target, &dst_path).await?;
                #[cfg(windows)]
                {
                    // On Windows, we need to determine if it's a file or directory symlink
                    // Try to get the target metadata to determine the type
                    if let Ok(target_metadata) = tokio::fs::metadata(&path).await {
                        if target_metadata.is_dir() {
                            tokio::fs::symlink_dir(&target, &dst_path).await?;
                        } else {
                            tokio::fs::symlink_file(&target, &dst_path).await?;
                        }
                    } else {
                        // If we can't determine the type, try as a file symlink
                        tokio::fs::symlink_file(&target, &dst_path).await?;
                    }
                }
            } else if metadata.is_dir() {
                copy_dir(&path, &dst_path).await?;
            } else {
                if let Some(parent) = dst_path.parent() {
                    create_dir(parent).await?;
                }
                copy_file(&path, &dst_path).await?;
            }
        }

        Ok(())
    })
}

/// Writes content to a file, creating parent directories if needed.
pub async fn write_file(path: &Path, content: &str) -> Result<()> {
    if let Some(parent) = path.parent() {
        create_dir(parent).await?;
    }

    // Generate unique temporary filename to avoid collisions
    let temp_path = {
        let mut temp = path.to_path_buf();
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();
        let pid = std::process::id();
        temp.set_file_name(format!(
            ".{}.{}.{}.tmp",
            path.file_name().unwrap_or_default().to_string_lossy(),
            pid,
            timestamp
        ));
        temp
    };

    tokio::fs::write(&temp_path, content).await?;

    match tokio::fs::rename(&temp_path, path).await {
        Ok(()) => Ok(()),
        Err(e) => {
            let _ = tokio::fs::remove_file(&temp_path).await;
            Err(e.into())
        }
    }
}

/// Reads the contents of a file as a string.
pub async fn read_file(path: &Path) -> Result<String> {
    let content = tokio::fs::read_to_string(path).await?;
    Ok(content)
}

/// Checks if a path exists (file or directory).
pub async fn exists(path: &Path) -> Result<bool> {
    Ok(tokio::fs::try_exists(path).await.unwrap_or(false))
}

/// Removes a directory and all its contents recursively.
pub async fn remove_dir(path: &Path) -> Result<()> {
    tokio::fs::remove_dir_all(path).await?;
    Ok(())
}

/// Removes a file.
pub async fn remove_file(path: &Path) -> Result<()> {
    tokio::fs::remove_file(path).await?;
    Ok(())
}

/// Copies a file from source to destination, preserving file permissions.
pub async fn copy_file(from: &Path, to: &Path) -> Result<()> {
    tokio::fs::copy(from, to).await?;
    let metadata = tokio::fs::metadata(from).await?;
    tokio::fs::set_permissions(to, metadata.permissions()).await?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_create_and_read_file() {
        let temp_dir = TempDir::new().unwrap();

        let file_path = temp_dir.path().join("test.txt");
        let content = "Hello, world!";

        // Write file
        write_file(&file_path, content).await.unwrap();

        // Read file
        let read_content = read_file(&file_path).await.unwrap();
        assert_eq!(read_content, content);

        // Check exists
        assert!(exists(&file_path).await.unwrap());
    }

    #[tokio::test]
    async fn test_create_dir() {
        let temp_dir = TempDir::new().unwrap();

        let dir_path = temp_dir.path().join("test_dir");

        // Create directory
        create_dir(&dir_path).await.unwrap();

        // Check exists
        assert!(exists(&dir_path).await.unwrap());
    }

    #[tokio::test]
    async fn test_copy_file() {
        let temp_dir = TempDir::new().unwrap();

        let source_path = temp_dir.path().join("source.txt");
        let dest_path = temp_dir.path().join("dest.txt");
        let content = "Test content";

        // Create source file
        write_file(&source_path, content).await.unwrap();

        // Copy file
        copy_file(&source_path, &dest_path).await.unwrap();

        // Verify both files exist and have same content
        assert!(exists(&source_path).await.unwrap());
        assert!(exists(&dest_path).await.unwrap());

        let dest_content = read_file(&dest_path).await.unwrap();
        assert_eq!(dest_content, content);
    }

    #[tokio::test]
    async fn test_copy_dir() {
        let temp_dir = TempDir::new().unwrap();

        let source_dir = temp_dir.path().join("source_dir");
        let dest_dir = temp_dir.path().join("dest_dir");

        // Create source directory structure
        create_dir(&source_dir).await.unwrap();
        write_file(&source_dir.join("file1.txt"), "content1")
            .await
            .unwrap();
        create_dir(&source_dir.join("subdir")).await.unwrap();
        write_file(&source_dir.join("subdir").join("file2.txt"), "content2")
            .await
            .unwrap();

        // Copy directory
        copy_dir(&source_dir, &dest_dir).await.unwrap();

        // Verify structure
        assert!(exists(&dest_dir).await.unwrap());
        assert!(exists(&dest_dir.join("file1.txt")).await.unwrap());
        assert!(exists(&dest_dir.join("subdir")).await.unwrap());
        assert!(
            exists(&dest_dir.join("subdir").join("file2.txt"))
                .await
                .unwrap()
        );

        // Verify content
        let content1 = read_file(&dest_dir.join("file1.txt")).await.unwrap();
        assert_eq!(content1, "content1");

        let content2 = read_file(&dest_dir.join("subdir").join("file2.txt"))
            .await
            .unwrap();
        assert_eq!(content2, "content2");
    }

    #[tokio::test]
    async fn test_copy_dir_with_symlinks() {
        let temp_dir = TempDir::new().unwrap();

        let source_dir = temp_dir.path().join("source_dir");
        let dest_dir = temp_dir.path().join("dest_dir");
        let target_dir = temp_dir.path().join("target_dir");

        // Create source directory structure
        create_dir(&source_dir).await.unwrap();
        write_file(&source_dir.join("regular_file.txt"), "regular content")
            .await
            .unwrap();

        // Create a target directory and file that symlinks will point to
        create_dir(&target_dir).await.unwrap();
        write_file(&target_dir.join("target_file.txt"), "target content")
            .await
            .unwrap();
        create_dir(&target_dir.join("target_subdir")).await.unwrap();
        write_file(
            &target_dir.join("target_subdir").join("nested.txt"),
            "nested content",
        )
        .await
        .unwrap();

        // Create symlinks in source directory
        #[cfg(unix)]
        {
            // Symlink to a file
            tokio::fs::symlink(
                &target_dir.join("target_file.txt"),
                &source_dir.join("symlink_to_file"),
            )
            .await
            .unwrap();

            // Symlink to a directory
            tokio::fs::symlink(
                &target_dir.join("target_subdir"),
                &source_dir.join("symlink_to_dir"),
            )
            .await
            .unwrap();

            // Relative symlink
            tokio::fs::symlink(
                "../target_dir/target_file.txt",
                &source_dir.join("relative_symlink"),
            )
            .await
            .unwrap();
        }

        #[cfg(windows)]
        {
            // On Windows, we need to specify file vs directory symlinks
            tokio::fs::symlink_file(
                &target_dir.join("target_file.txt"),
                &source_dir.join("symlink_to_file"),
            )
            .await
            .unwrap();

            tokio::fs::symlink_dir(
                &target_dir.join("target_subdir"),
                &source_dir.join("symlink_to_dir"),
            )
            .await
            .unwrap();

            tokio::fs::symlink_file(
                "../target_dir/target_file.txt",
                &source_dir.join("relative_symlink"),
            )
            .await
            .unwrap();
        }

        // Copy directory with symlinks
        copy_dir(&source_dir, &dest_dir).await.unwrap();

        // Verify regular file was copied
        assert!(exists(&dest_dir.join("regular_file.txt")).await.unwrap());
        let content = read_file(&dest_dir.join("regular_file.txt")).await.unwrap();
        assert_eq!(content, "regular content");

        // Verify symlinks were preserved as symlinks (not dereferenced)
        #[cfg(unix)]
        {
            // Check file symlink
            let symlink_metadata = tokio::fs::symlink_metadata(&dest_dir.join("symlink_to_file"))
                .await
                .unwrap();
            assert!(symlink_metadata.is_symlink());

            // Check directory symlink
            let dir_symlink_metadata =
                tokio::fs::symlink_metadata(&dest_dir.join("symlink_to_dir"))
                    .await
                    .unwrap();
            assert!(dir_symlink_metadata.is_symlink());

            // Check relative symlink
            let rel_symlink_metadata =
                tokio::fs::symlink_metadata(&dest_dir.join("relative_symlink"))
                    .await
                    .unwrap();
            assert!(rel_symlink_metadata.is_symlink());

            // Verify symlink targets are preserved
            let link_target = tokio::fs::read_link(&dest_dir.join("symlink_to_file"))
                .await
                .unwrap();
            assert_eq!(link_target, target_dir.join("target_file.txt"));

            let dir_link_target = tokio::fs::read_link(&dest_dir.join("symlink_to_dir"))
                .await
                .unwrap();
            assert_eq!(dir_link_target, target_dir.join("target_subdir"));

            let rel_link_target = tokio::fs::read_link(&dest_dir.join("relative_symlink"))
                .await
                .unwrap();
            assert_eq!(
                rel_link_target.to_string_lossy(),
                "../target_dir/target_file.txt"
            );
        }

        #[cfg(windows)]
        {
            // On Windows, check that symlinks exist and point to correct targets
            let symlink_metadata = tokio::fs::symlink_metadata(&dest_dir.join("symlink_to_file"))
                .await
                .unwrap();
            assert!(symlink_metadata.is_symlink());

            let dir_symlink_metadata =
                tokio::fs::symlink_metadata(&dest_dir.join("symlink_to_dir"))
                    .await
                    .unwrap();
            assert!(dir_symlink_metadata.is_symlink());
        }
    }

    #[tokio::test]
    async fn test_remove_dir() {
        let temp_dir = TempDir::new().unwrap();

        let test_dir = temp_dir.path().join("test_dir");

        // Create directory with content
        create_dir(&test_dir).await.unwrap();
        write_file(&test_dir.join("file.txt"), "content")
            .await
            .unwrap();

        // Verify it exists
        assert!(exists(&test_dir).await.unwrap());

        // Remove directory
        remove_dir(&test_dir).await.unwrap();

        // Verify it's gone
        assert!(!exists(&test_dir).await.unwrap());
    }

    #[tokio::test]
    async fn test_remove_file() {
        let temp_dir = TempDir::new().unwrap();

        let file_path = temp_dir.path().join("test.txt");
        let content = "Test content";

        // Create file
        write_file(&file_path, content).await.unwrap();

        // Verify it exists
        assert!(exists(&file_path).await.unwrap());

        // Remove file
        remove_file(&file_path).await.unwrap();

        // Verify it's gone
        assert!(!exists(&file_path).await.unwrap());

        // Try to remove non-existent file - should error
        let result = remove_file(&file_path).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_concurrent_write_and_read_safety() {
        use tokio::task;

        let temp_dir = TempDir::new().unwrap();
        let file_path = Arc::new(temp_dir.path().join("concurrent_test.json"));

        // Create initial file with valid JSON
        let initial_content = r#"{"tasks": []}"#;
        write_file(&file_path, initial_content).await.unwrap();

        // Spawn multiple concurrent writers and readers
        let mut handles = vec![];

        // Writers - continuously update the file with valid JSON
        for i in 0..5 {
            let path_clone = file_path.clone();
            handles.push(task::spawn(async move {
                for j in 0..20 {
                    let content = format!(r#"{{"task_id": {}, "iteration": {}}}"#, i, j);
                    write_file(&path_clone, &content).await.unwrap();
                    tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
                }
            }));
        }

        // Readers - continuously read and parse the file
        for _ in 0..5 {
            let path_clone = file_path.clone();
            handles.push(task::spawn(async move {
                for _ in 0..50 {
                    let content = read_file(&path_clone).await.unwrap();
                    // Should always be valid JSON - never empty or partial
                    assert!(!content.is_empty(), "File should never be empty");
                    // Verify it's valid JSON
                    let _: serde_json::Value = serde_json::from_str(&content)
                        .expect("File should always contain valid JSON");
                    tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
                }
            }));
        }

        // Wait for all tasks to complete
        for handle in handles {
            handle.await.unwrap();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_copy_file_preserves_permissions() {
        use std::os::unix::fs::PermissionsExt;

        let temp_dir = TempDir::new().unwrap();

        let source_path = temp_dir.path().join("executable.sh");
        let dest_path = temp_dir.path().join("executable_copy.sh");

        write_file(&source_path, "#!/bin/bash\necho hello")
            .await
            .unwrap();

        // Make the source file executable
        let perms = std::fs::Permissions::from_mode(0o755);
        tokio::fs::set_permissions(&source_path, perms)
            .await
            .unwrap();

        copy_file(&source_path, &dest_path).await.unwrap();

        let dest_metadata = tokio::fs::metadata(&dest_path).await.unwrap();
        let dest_mode = dest_metadata.permissions().mode();
        assert_eq!(
            dest_mode & 0o777,
            0o755,
            "Destination file should preserve executable permissions"
        );
    }
}