lingshu-tools 0.10.0

Tool registry, ToolHandler trait, and 50+ tool implementations
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
//! # Singularity / Apptainer execution backend
//!
//! Executes commands inside a persistent Apptainer/Singularity instance.
//!
//! Design goals:
//! - actionable preflight errors when the runtime is missing
//! - task-scoped persistent instances
//! - persistent overlays when requested
//! - interrupt / timeout handling aligned with the other backends

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use async_trait::async_trait;
use tokio::process::Command;
use tokio::sync::Mutex as TokioMutex;
use tokio_util::sync::CancellationToken;
use tracing::info;

use lingshu_types::ToolError;

use crate::execution_tmp::{BACKEND_TMP_ROOT, wrap_command_with_tmp_env};

use super::{
    BackendKind, ExecOutput, ExecutionBackend, SingularityBackendConfig, ensure_dir,
    sandbox_root_dir, sanitize_resource_name, shell_quote,
};

fn resolve_executable() -> Result<String, ToolError> {
    if let Ok(explicit) = std::env::var("EDGECRAB_SINGULARITY_BIN")
        && !explicit.trim().is_empty()
    {
        return Ok(explicit);
    }

    if let Ok(path) = which::which("apptainer") {
        return Ok(path.to_string_lossy().into_owned());
    }
    if let Ok(path) = which::which("singularity") {
        return Ok(path.to_string_lossy().into_owned());
    }

    Err(ToolError::ExecutionFailed {
        tool: "terminal".into(),
        message: "Singularity backend selected but neither `apptainer` nor `singularity` was found in PATH. Install Apptainer or set EDGECRAB_SINGULARITY_BIN.".into(),
    })
}

async fn verify_executable(executable: &str) -> Result<(), ToolError> {
    let output = Command::new(executable)
        .arg("version")
        .kill_on_drop(true)
        .output()
        .await
        .map_err(|e| ToolError::ExecutionFailed {
            tool: "terminal".into(),
            message: format!("Failed to run `{executable} version`: {e}"),
        })?;

    if output.status.success() {
        return Ok(());
    }

    Err(ToolError::ExecutionFailed {
        tool: "terminal".into(),
        message: format!(
            "`{executable} version` failed: {}",
            String::from_utf8_lossy(&output.stderr).trim()
        ),
    })
}

fn overlay_dir(task_id: &str) -> Result<std::path::PathBuf, ToolError> {
    let dir = sandbox_root_dir()
        .join("singularity")
        .join(format!("overlay-{}", sanitize_resource_name(task_id)));
    ensure_dir(&dir, "singularity overlay")?;
    Ok(dir)
}

#[derive(Debug)]
struct SingularityState {
    executable: String,
    instance_name: String,
    dead: Arc<AtomicBool>,
}

impl SingularityState {
    async fn new(cfg: &SingularityBackendConfig, task_id: &str) -> Result<Self, ToolError> {
        let executable = resolve_executable()?;
        verify_executable(&executable).await?;

        let instance_name = format!("lingshu-{}", sanitize_resource_name(task_id));

        // Make startup idempotent across stale instances.
        let _ = Command::new(&executable)
            .args(["instance", "stop", &instance_name])
            .kill_on_drop(true)
            .output()
            .await;

        let mut cmd = Command::new(&executable);
        cmd.arg("instance")
            .arg("start")
            .arg("--containall")
            .arg("--no-home");

        if cfg.persistent_filesystem {
            let overlay = overlay_dir(task_id)?;
            cmd.arg("--overlay").arg(overlay);
        } else {
            cmd.arg("--writable-tmpfs");
        }

        if cfg.memory_mb > 0 {
            cmd.arg("--memory").arg(format!("{}M", cfg.memory_mb));
        }
        if cfg.cpu > 0.0 {
            cmd.arg("--cpus").arg(cfg.cpu.to_string());
        }

        let output = cmd
            .arg(&cfg.image)
            .arg(&instance_name)
            .kill_on_drop(true)
            .output()
            .await
            .map_err(|e| ToolError::ExecutionFailed {
                tool: "terminal".into(),
                message: format!("Failed to start Singularity instance: {e}"),
            })?;

        if !output.status.success() {
            return Err(ToolError::ExecutionFailed {
                tool: "terminal".into(),
                message: format!(
                    "Singularity instance start failed: {}",
                    String::from_utf8_lossy(&output.stderr).trim()
                ),
            });
        }

        info!("SingularityBackend: started instance {instance_name}");

        Ok(Self {
            executable,
            instance_name,
            dead: Arc::new(AtomicBool::new(false)),
        })
    }

    async fn exec(
        &self,
        command: &str,
        cwd: &str,
        timeout: Duration,
        cancel: CancellationToken,
    ) -> Result<ExecOutput, ToolError> {
        if self.dead.load(Ordering::Relaxed) {
            return Err(ToolError::ExecutionFailed {
                tool: "terminal".into(),
                message: "Singularity instance is stopped".into(),
            });
        }

        let mut workdir = if cwd.is_empty() { "/tmp" } else { cwd }.to_string();
        let mut exec_command = command.to_string();
        if workdir == "~" || workdir.starts_with("~/") {
            exec_command = format!("cd {} && {}", shell_quote(&workdir), exec_command);
            workdir = "/tmp".into();
        }
        exec_command = wrap_command_with_tmp_env(&exec_command, BACKEND_TMP_ROOT);

        let mut cmd = Command::new(&self.executable);
        cmd.arg("exec")
            .arg("--pwd")
            .arg(&workdir)
            .arg(format!("instance://{}", self.instance_name))
            .arg("bash")
            .arg("-c")
            .arg(exec_command)
            .kill_on_drop(true);

        let fut = cmd.output();
        tokio::pin!(fut);

        tokio::select! {
            res = tokio::time::timeout(timeout, &mut fut) => {
                match res {
                    Ok(Ok(output)) => Ok(ExecOutput {
                        stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
                        stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
                        exit_code: output.status.code().unwrap_or(-1),
                    }),
                    Ok(Err(e)) => Err(ToolError::ExecutionFailed {
                        tool: "terminal".into(),
                        message: format!("Singularity exec failed: {e}"),
                    }),
                    Err(_) => Ok(ExecOutput {
                        stdout: String::new(),
                        stderr: String::new(),
                        exit_code: 124,
                    }),
                }
            }
            _ = cancel.cancelled() => Ok(ExecOutput {
                stdout: String::new(),
                stderr: String::new(),
                exit_code: 130,
            })
        }
    }

    async fn stop(&self) {
        let _ = Command::new(&self.executable)
            .args(["instance", "stop", &self.instance_name])
            .kill_on_drop(true)
            .output()
            .await;
        self.dead.store(true, Ordering::Relaxed);
    }
}

pub struct SingularityBackend {
    config: SingularityBackendConfig,
    task_id: String,
    state: TokioMutex<Option<Arc<SingularityState>>>,
}

impl SingularityBackend {
    pub fn new(task_id: impl Into<String>, config: SingularityBackendConfig) -> Self {
        Self {
            config,
            task_id: task_id.into(),
            state: TokioMutex::new(None),
        }
    }

    async fn ensure_state(&self) -> Result<Arc<SingularityState>, ToolError> {
        let mut guard = self.state.lock().await;
        let needs_init = guard
            .as_ref()
            .map(|s| s.dead.load(Ordering::Relaxed))
            .unwrap_or(true);
        if needs_init {
            *guard = Some(Arc::new(
                SingularityState::new(&self.config, &self.task_id).await?,
            ));
        }
        guard.clone().ok_or_else(|| ToolError::ExecutionFailed {
            tool: "terminal".into(),
            message: "Singularity state missing after init".into(),
        })
    }
}

#[async_trait]
impl ExecutionBackend for SingularityBackend {
    async fn execute(
        &self,
        command: &str,
        cwd: &str,
        timeout: Duration,
        cancel: CancellationToken,
        options: super::ExecuteOptions,
    ) -> Result<ExecOutput, ToolError> {
        let state = self.ensure_state().await?;
        super::start_execute_progress(&options, "Singularity", command);
        let output = state.exec(command, cwd, timeout, cancel).await?;
        super::finalize_execute_progress(&options, &output);
        Ok(output)
    }

    async fn execute_oneshot(
        &self,
        command: &str,
        cwd: &str,
        timeout: Duration,
        cancel: CancellationToken,
    ) -> Result<ExecOutput, ToolError> {
        let state = self.ensure_state().await?;
        state.exec(command, cwd, timeout, cancel).await
    }

    async fn cleanup(&self) -> Result<(), ToolError> {
        let mut guard = self.state.lock().await;
        if let Some(state) = guard.take() {
            if let Ok(state) = Arc::try_unwrap(state) {
                state.stop().await;
            } else {
                tracing::warn!(
                    task_id = %self.task_id,
                    "Singularity backend cleanup deferred because commands are still holding the instance"
                );
            }
        }
        Ok(())
    }

    fn kind(&self) -> BackendKind {
        BackendKind::Singularity
    }

    fn supports_remote_execute_code(&self) -> bool {
        true
    }

    async fn is_healthy(&self) -> bool {
        let guard = self.state.lock().await;
        match guard.as_ref() {
            Some(s) => !s.dead.load(Ordering::Relaxed),
            None => false,
        }
    }
}

#[cfg(test)]
pub(crate) static SINGULARITY_TEST_ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(unix)]
    fn write_fake_runtime(dir: &tempfile::TempDir, body: &str) -> std::path::PathBuf {
        use std::os::unix::fs::PermissionsExt;

        let path = dir.path().join("fake-apptainer");
        std::fs::write(&path, body).expect("write fake runtime");
        let mut perms = std::fs::metadata(&path).expect("metadata").permissions();
        perms.set_mode(0o755);
        std::fs::set_permissions(&path, perms).expect("chmod");
        path
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn missing_runtime_is_actionable() {
        let _guard = SINGULARITY_TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        unsafe { std::env::set_var("EDGECRAB_SINGULARITY_BIN", "/definitely/missing/apptainer") };
        let err = SingularityState::new(&SingularityBackendConfig::default(), "missing")
            .await
            .expect_err("missing runtime must fail");
        let msg = err.to_string();
        assert!(msg.contains("Failed to run"), "got: {msg}");
        unsafe { std::env::remove_var("EDGECRAB_SINGULARITY_BIN") };
    }

    #[cfg(unix)]
    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn fake_runtime_executes_and_cleans_up() {
        let _guard = SINGULARITY_TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let dir = tempfile::tempdir().expect("tempdir");
        let log = dir.path().join("runtime.log");
        let fake = write_fake_runtime(
            &dir,
            r#"#!/bin/sh
set -eu
log="${EDGECRAB_SINGULARITY_TEST_LOG:-}"
case "${1:-}" in
  version)
    echo "apptainer 1.0.0"
    ;;
  instance)
    echo "instance:$*" >> "$log"
    ;;
  exec)
    echo "exec:$*" >> "$log"
    last=""
    for arg in "$@"; do last="$arg"; done
    case "$last" in
      *"&& exit 7")
      exit 7
      ;;
    esac
    printf 'sg:%s\n' "$last"
    ;;
  *)
    echo "unexpected:$*" >> "$log"
    ;;
esac
"#,
        );

        unsafe {
            std::env::set_var("EDGECRAB_SINGULARITY_BIN", &fake);
            std::env::set_var("EDGECRAB_SINGULARITY_TEST_LOG", &log);
        }

        let backend = SingularityBackend::new("sg-e2e", SingularityBackendConfig::default());
        let out = backend
            .execute(
                "echo hello-singularity",
                "/workspace",
                Duration::from_secs(2),
                CancellationToken::new(),
                crate::tools::backends::ExecuteOptions::default(),
            )
            .await
            .expect("execute");
        assert!(
            out.stdout.contains("sg:mkdir -p '/tmp/lingshu-tmp'"),
            "got: {out:?}"
        );
        assert!(
            out.stdout.contains("EDGECRAB_TMPDIR='/tmp/lingshu-tmp'"),
            "got: {out:?}"
        );
        assert!(
            out.stdout.contains("&& echo hello-singularity"),
            "got: {out:?}"
        );

        let out = backend
            .execute(
                "exit 7",
                "/workspace",
                Duration::from_secs(2),
                CancellationToken::new(),
                crate::tools::backends::ExecuteOptions::default(),
            )
            .await
            .expect("execute exit");
        assert_eq!(out.exit_code, 7);

        backend.cleanup().await.expect("cleanup");
        let logged = std::fs::read_to_string(&log).expect("read log");
        assert!(logged.contains("instance:instance start"), "got: {logged}");
        assert!(logged.contains("instance:instance stop"), "got: {logged}");

        unsafe {
            std::env::remove_var("EDGECRAB_SINGULARITY_BIN");
            std::env::remove_var("EDGECRAB_SINGULARITY_TEST_LOG");
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn fake_runtime_timeout_returns_124() {
        let _guard = SINGULARITY_TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let dir = tempfile::tempdir().expect("tempdir");
        let fake = write_fake_runtime(
            &dir,
            r#"#!/bin/sh
set -eu
case "${1:-}" in
  version) echo ok ;;
  instance) exit 0 ;;
  exec) sleep 5 ;;
esac
"#,
        );
        unsafe { std::env::set_var("EDGECRAB_SINGULARITY_BIN", &fake) };

        let backend = SingularityBackend::new("sg-timeout", SingularityBackendConfig::default());
        let out = backend
            .execute(
                "sleep forever",
                "/workspace",
                Duration::from_millis(50),
                CancellationToken::new(),
                crate::tools::backends::ExecuteOptions::default(),
            )
            .await
            .expect("timeout execute");
        assert_eq!(out.exit_code, 124);
        backend.cleanup().await.expect("cleanup");

        unsafe { std::env::remove_var("EDGECRAB_SINGULARITY_BIN") };
    }
}