stakpak 0.3.58

Stakpak: Your DevOps AI Agent. Generate infrastructure code, debug Kubernetes, configure CI/CD, automate deployments, without giving an LLM the keys to production.
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
//! Check script executor with timeout support.
//!
//! Executes check scripts as child processes, capturing output and enforcing timeouts.

use std::path::Path;
use std::process::Stdio;
use std::time::Duration;
use tokio::io::AsyncReadExt;
use tokio::process::Command;
use tracing::{debug, warn};

/// Result of running a check script.
#[derive(Debug, Clone)]
pub struct CheckResult {
    /// Exit code of the script (None if killed/timed out before exit).
    pub exit_code: Option<i32>,
    /// Standard output captured from the script.
    pub stdout: String,
    /// Standard error captured from the script.
    pub stderr: String,
    /// Whether the script was killed due to timeout.
    pub timed_out: bool,
}

impl CheckResult {
    /// Returns true if the check passed (exit code 0).
    pub fn passed(&self) -> bool {
        self.exit_code == Some(0)
    }

    /// Returns true if the check was skipped (exit code 1).
    pub fn skipped(&self) -> bool {
        self.exit_code == Some(1)
    }

    /// Returns true if the check failed (exit code 2+ or timeout).
    pub fn failed(&self) -> bool {
        self.timed_out || matches!(self.exit_code, Some(code) if code >= 2)
    }
}

/// Errors that can occur during check script execution.
#[derive(Debug, thiserror::Error)]
pub enum ExecutorError {
    #[error("Script not found: {0}")]
    ScriptNotFound(String),

    #[error("Script is not executable: {0}")]
    NotExecutable(String),

    #[error("Failed to spawn process: {0}")]
    SpawnError(String),

    #[error("Failed to read output: {0}")]
    OutputError(String),
}

const ETXTBSY_ERRNO: i32 = 26;
const SPAWN_RETRY_ATTEMPTS: usize = 20;
const SPAWN_RETRY_DELAY: Duration = Duration::from_millis(50);

fn is_text_file_busy(error: &std::io::Error) -> bool {
    #[cfg(unix)]
    {
        error.raw_os_error() == Some(ETXTBSY_ERRNO)
    }

    #[cfg(not(unix))]
    {
        let _ = error;
        false
    }
}

async fn spawn_check_script(path: &Path) -> Result<tokio::process::Child, ExecutorError> {
    let mut attempt = 1;

    loop {
        match Command::new(path)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .kill_on_drop(true)
            .spawn()
        {
            Ok(child) => return Ok(child),
            Err(error) if is_text_file_busy(&error) && attempt < SPAWN_RETRY_ATTEMPTS => {
                debug!(
                    script = %path.display(),
                    attempt,
                    retry_delay_ms = SPAWN_RETRY_DELAY.as_millis(),
                    "Script spawn hit ETXTBSY; retrying"
                );
                attempt += 1;
                tokio::time::sleep(SPAWN_RETRY_DELAY).await;
            }
            Err(error) => return Err(ExecutorError::SpawnError(error.to_string())),
        }
    }
}

/// Run a check script with timeout enforcement.
///
/// # Arguments
/// * `path` - Path to the script to execute
/// * `timeout` - Maximum time to wait for the script to complete
///
/// # Returns
/// * `Ok(CheckResult)` - Script completed (possibly with timeout)
/// * `Err(ExecutorError)` - Failed to run the script
pub async fn run_check_script(
    path: &Path,
    timeout: Duration,
) -> Result<CheckResult, ExecutorError> {
    // Verify script exists
    if !path.exists() {
        return Err(ExecutorError::ScriptNotFound(path.display().to_string()));
    }

    // On Unix, verify script is executable
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let metadata =
            std::fs::metadata(path).map_err(|e| ExecutorError::ScriptNotFound(e.to_string()))?;
        let permissions = metadata.permissions();
        if permissions.mode() & 0o111 == 0 {
            return Err(ExecutorError::NotExecutable(path.display().to_string()));
        }
    }

    debug!(script = %path.display(), timeout_secs = timeout.as_secs(), "Running check script");

    // Spawn the process (retry ETXTBSY transient errors on Unix CI filesystems)
    let mut child = spawn_check_script(path).await?;

    // Take ownership of stdout/stderr handles
    let mut stdout_handle = child.stdout.take();
    let mut stderr_handle = child.stderr.take();

    // Run with timeout
    let result = tokio::time::timeout(timeout, async {
        // Read stdout and stderr concurrently
        let stdout_future = async {
            let mut buf = Vec::new();
            if let Some(ref mut handle) = stdout_handle {
                handle.read_to_end(&mut buf).await.ok();
            }
            String::from_utf8_lossy(&buf).to_string()
        };

        let stderr_future = async {
            let mut buf = Vec::new();
            if let Some(ref mut handle) = stderr_handle {
                handle.read_to_end(&mut buf).await.ok();
            }
            String::from_utf8_lossy(&buf).to_string()
        };

        let (stdout, stderr) = tokio::join!(stdout_future, stderr_future);

        // Wait for the process to exit
        let status = child.wait().await;

        (stdout, stderr, status)
    })
    .await;

    match result {
        Ok((stdout, stderr, status)) => {
            let exit_code = status.ok().and_then(|s| s.code());

            debug!(
                script = %path.display(),
                exit_code = ?exit_code,
                stdout_len = stdout.len(),
                stderr_len = stderr.len(),
                "Check script completed"
            );

            Ok(CheckResult {
                exit_code,
                stdout,
                stderr,
                timed_out: false,
            })
        }
        Err(_) => {
            // Timeout occurred - process will be killed by kill_on_drop
            warn!(
                script = %path.display(),
                timeout_secs = timeout.as_secs(),
                "Check script timed out"
            );

            // Try to read any partial output
            let stdout = if let Some(mut handle) = stdout_handle {
                let mut buf = Vec::new();
                handle.read_to_end(&mut buf).await.ok();
                String::from_utf8_lossy(&buf).to_string()
            } else {
                String::new()
            };

            let stderr = if let Some(mut handle) = stderr_handle {
                let mut buf = Vec::new();
                handle.read_to_end(&mut buf).await.ok();
                String::from_utf8_lossy(&buf).to_string()
            } else {
                String::new()
            };

            Ok(CheckResult {
                exit_code: None,
                stdout,
                stderr,
                timed_out: true,
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(unix)]
    use std::fs::{self, File};
    #[cfg(unix)]
    use std::io::Write;
    #[cfg(unix)]
    use tempfile::tempdir;

    #[cfg(unix)]
    use std::os::unix::fs::PermissionsExt;

    /// Create a test script that exits with the given code.
    #[cfg(unix)]
    fn create_script(dir: &Path, name: &str, content: &str) -> std::path::PathBuf {
        let script_path = dir.join(name);
        {
            let mut file = File::create(&script_path).expect("Failed to create script");
            file.write_all(content.as_bytes())
                .expect("Failed to write script");
            file.sync_all().expect("Failed to sync script");
            // file is dropped (closed) here before we try to execute it
        }

        #[cfg(unix)]
        {
            let mut perms = fs::metadata(&script_path)
                .expect("Failed to get metadata")
                .permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&script_path, perms).expect("Failed to set permissions");
        }

        script_path
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_script_exit_0() {
        let dir = tempdir().expect("Failed to create temp dir");
        let script = create_script(
            dir.path(),
            "check1.sh",
            "#!/bin/sh\necho 'context data'\nexit 0\n",
        );

        let result = run_check_script(&script, Duration::from_secs(5))
            .await
            .expect("Should run script");

        assert_eq!(result.exit_code, Some(0));
        assert!(result.passed());
        assert!(!result.skipped());
        assert!(!result.failed());
        assert!(!result.timed_out);
        assert!(result.stdout.contains("context data"));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_script_exit_1() {
        let dir = tempdir().expect("Failed to create temp dir");
        let script = create_script(
            dir.path(),
            "check2.sh",
            "#!/bin/sh\necho 'not ready'\nexit 1\n",
        );

        let result = run_check_script(&script, Duration::from_secs(5))
            .await
            .expect("Should run script");

        assert_eq!(result.exit_code, Some(1));
        assert!(!result.passed());
        assert!(result.skipped());
        assert!(!result.failed());
        assert!(!result.timed_out);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_script_exit_2() {
        let dir = tempdir().expect("Failed to create temp dir");
        let script = create_script(
            dir.path(),
            "check3.sh",
            "#!/bin/sh\necho 'error occurred' >&2\nexit 2\n",
        );

        let result = run_check_script(&script, Duration::from_secs(5))
            .await
            .expect("Should run script");

        assert_eq!(result.exit_code, Some(2));
        assert!(!result.passed());
        assert!(!result.skipped());
        assert!(result.failed());
        assert!(!result.timed_out);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_script_timeout() {
        let dir = tempdir().expect("Failed to create temp dir");
        let script = create_script(dir.path(), "check4.sh", "#!/bin/sh\nsleep 10\nexit 0\n");

        let result = run_check_script(&script, Duration::from_millis(100))
            .await
            .expect("Should run script");

        assert!(result.timed_out);
        assert!(result.failed());
        assert!(result.exit_code.is_none());
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_capture_stderr() {
        let dir = tempdir().expect("Failed to create temp dir");
        let script = create_script(
            dir.path(),
            "check5.sh",
            "#!/bin/sh\necho 'stdout message'\necho 'stderr message' >&2\nexit 0\n",
        );

        let result = run_check_script(&script, Duration::from_secs(5))
            .await
            .expect("Should run script");

        assert!(result.stdout.contains("stdout message"));
        assert!(result.stderr.contains("stderr message"));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_missing_script() {
        let result =
            run_check_script(Path::new("/nonexistent/script.sh"), Duration::from_secs(5)).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ExecutorError::ScriptNotFound(_)
        ));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_non_executable() {
        let dir = tempdir().expect("Failed to create temp dir");
        let script_path = dir.path().join("check6.sh");

        // Create file without execute permission
        {
            let mut file = File::create(&script_path).expect("Failed to create script");
            file.write_all(b"#!/bin/sh\nexit 0\n")
                .expect("Failed to write script");
            file.sync_all().expect("Failed to sync script");
            // file is dropped (closed) here before metadata/permission operations
        }

        let mut perms = fs::metadata(&script_path)
            .expect("Failed to get metadata")
            .permissions();
        perms.set_mode(0o644); // No execute permission
        fs::set_permissions(&script_path, perms).expect("Failed to set permissions");

        let result = run_check_script(&script_path, Duration::from_secs(5)).await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ExecutorError::NotExecutable(_)
        ));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_large_output() {
        let dir = tempdir().expect("Failed to create temp dir");
        // Generate a script that outputs a lot of data
        let script = create_script(
            dir.path(),
            "check7.sh",
            "#!/bin/sh\nfor i in $(seq 1 1000); do echo \"line $i\"; done\nexit 0\n",
        );

        let result = run_check_script(&script, Duration::from_secs(10))
            .await
            .expect("Should run script");

        assert_eq!(result.exit_code, Some(0));
        assert!(result.stdout.lines().count() >= 1000);
    }

    #[tokio::test]
    async fn test_check_result_methods() {
        // Test passed
        let passed = CheckResult {
            exit_code: Some(0),
            stdout: String::new(),
            stderr: String::new(),
            timed_out: false,
        };
        assert!(passed.passed());
        assert!(!passed.skipped());
        assert!(!passed.failed());

        // Test skipped
        let skipped = CheckResult {
            exit_code: Some(1),
            stdout: String::new(),
            stderr: String::new(),
            timed_out: false,
        };
        assert!(!skipped.passed());
        assert!(skipped.skipped());
        assert!(!skipped.failed());

        // Test failed (exit code 2)
        let failed = CheckResult {
            exit_code: Some(2),
            stdout: String::new(),
            stderr: String::new(),
            timed_out: false,
        };
        assert!(!failed.passed());
        assert!(!failed.skipped());
        assert!(failed.failed());

        // Test timed out
        let timed_out = CheckResult {
            exit_code: None,
            stdout: String::new(),
            stderr: String::new(),
            timed_out: true,
        };
        assert!(!timed_out.passed());
        assert!(!timed_out.skipped());
        assert!(timed_out.failed());
    }
}