kaish-kernel 0.7.0

Core kernel for kaish: lexer, parser, interpreter, and runtime
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
//! env — Print environment or run command with modified environment.
//!
//! # Examples
//!
//! ```kaish
//! env                           # Print all environment variables
//! env -0                        # Print with null separator (for xargs -0)
//! env VAR=value command args    # Run command with VAR set
//! env -i command                # Run command with empty environment
//! env -u VAR command            # Run command with VAR unset
//! ```

use async_trait::async_trait;

use crate::ast::Value;
use crate::interpreter::{ExecResult, OutputData};
use crate::tools::{ExecContext, ParamSchema, Tool, ToolArgs, ToolSchema};

/// Env tool: print environment or run command with modified environment.
pub struct Env;

#[async_trait]
impl Tool for Env {
    fn name(&self) -> &str {
        "env"
    }

    fn schema(&self) -> ToolSchema {
        ToolSchema::new(
            "env",
            "Print environment variables or run command with modified environment",
        )
        .param(ParamSchema::optional(
            "args",
            "array",
            Value::Null,
            "VAR=value pairs followed by optional command and arguments",
        ))
        .param(ParamSchema::optional(
            "0",
            "bool",
            Value::Bool(false),
            "Use NUL as separator instead of newline (-0)",
        ))
        .param(ParamSchema::optional(
            "i",
            "bool",
            Value::Bool(false),
            "Start with empty environment (-i)",
        ))
        .param(ParamSchema::optional(
            "u",
            "string",
            Value::Null,
            "Unset variable from environment (-u VAR)",
        ))
        .example("Print environment", "env")
        .example("Run with modified env", "env MY_VAR=hello command")
    }

    async fn execute(&self, args: ToolArgs, ctx: &mut ExecContext) -> ExecResult {
        let null_sep = args.has_flag("0");
        let clear_env = args.has_flag("i");

        // Collect -u (unset) value if specified
        let unset_vars: Vec<String> = args
            .get_named("u")
            .iter()
            .filter_map(|v| match v {
                Value::String(s) => Some(s.clone()),
                _ => None,
            })
            .collect();

        // No positional arguments: just print environment
        if args.positional.is_empty() {
            return print_env(ctx, null_sep);
        }

        // Parse arguments: VAR=value pairs, then optional command
        let mut env_overrides: Vec<(String, String)> = Vec::new();
        let mut command_start = None;

        for (i, arg) in args.positional.iter().enumerate() {
            let arg_str = match arg {
                Value::String(s) => s.as_str(),
                _ => {
                    command_start = Some(i);
                    break;
                }
            };

            // Check if it's a VAR=value assignment
            if let Some(eq_pos) = arg_str.find('=') {
                let name = &arg_str[..eq_pos];
                let value = &arg_str[eq_pos + 1..];
                env_overrides.push((name.to_string(), value.to_string()));
            } else {
                // Not an assignment, start of command
                command_start = Some(i);
                break;
            }
        }

        // If no command, just print environment with overrides
        if command_start.is_none() {
            return print_env_with_overrides(ctx, &env_overrides, &unset_vars, clear_env, null_sep);
        }

        // Execute command with modified environment
        // Safety: we checked `command_start.is_none()` above and returned
        let Some(cmd_idx) = command_start else {
            return ExecResult::failure(1, "env: internal error: missing command index");
        };
        let command = match &args.positional[cmd_idx] {
            Value::String(s) => s.clone(),
            other => value_to_string(other),
        };

        let cmd_args: Vec<String> = args.positional[cmd_idx + 1..]
            .iter()
            .map(value_to_string)
            .collect();

        #[cfg(feature = "native")]
        {
            return execute_with_env(
                ctx,
                &command,
                &cmd_args,
                &env_overrides,
                &unset_vars,
                clear_env,
            )
            .await;
        }

        #[cfg(not(feature = "native"))]
        {
            let _ = (ctx, &command, &cmd_args, &env_overrides, &unset_vars, clear_env);
            return ExecResult::failure(1, "env: external commands not available in sandbox mode");
        }
    }
}

/// Print current environment variables.
fn print_env(ctx: &ExecContext, null_sep: bool) -> ExecResult {
    // Build sorted list of vars
    let mut vars: Vec<_> = ctx.scope.exported_vars().into_iter().collect();
    vars.sort_by(|(a, _), (b, _)| a.cmp(b));

    let sep = if null_sep { '\0' } else { '\n' };
    let mut output = String::new();

    for (name, value) in vars {
        let value_str = value_to_string(&value);
        output.push_str(&format!("{}={}{}", name, value_str, sep));
    }

    // Remove trailing separator for consistency (unless null-sep)
    if !null_sep && !output.is_empty() {
        output.pop();
    }

    ExecResult::with_output(OutputData::text(output))
}

/// Print environment with overrides applied.
fn print_env_with_overrides(
    ctx: &ExecContext,
    overrides: &[(String, String)],
    unset: &[String],
    clear: bool,
    null_sep: bool,
) -> ExecResult {
    // Start with existing exports unless clearing
    let mut env: std::collections::HashMap<String, String> = if clear {
        std::collections::HashMap::new()
    } else {
        ctx.scope
            .exported_vars()
            .into_iter()
            .map(|(k, v)| (k, value_to_string(&v)))
            .collect()
    };

    // Remove unset variables
    for name in unset {
        env.remove(name);
    }

    // Apply overrides
    for (name, value) in overrides {
        env.insert(name.clone(), value.clone());
    }

    // Sort pairs
    let mut pairs: Vec<_> = env.into_iter().collect();
    pairs.sort_by(|(a, _), (b, _)| a.cmp(b));

    let sep = if null_sep { '\0' } else { '\n' };
    let mut output = String::new();

    for (name, value) in pairs {
        output.push_str(&format!("{}={}{}", name, value, sep));
    }

    // Remove trailing separator for consistency (unless null-sep)
    if !null_sep && !output.is_empty() {
        output.pop();
    }

    ExecResult::with_output(OutputData::text(output))
}

/// Execute a command with modified environment.
#[cfg(feature = "native")]
async fn execute_with_env(
    ctx: &mut ExecContext,
    command: &str,
    args: &[String],
    overrides: &[(String, String)],
    unset: &[String],
    clear: bool,
) -> ExecResult {
    use tokio::process::Command;
    let mut cmd = Command::new(command);
    cmd.args(args);

    if clear {
        cmd.env_clear();
    } else {
        // Set exported variables from scope
        for (name, value) in ctx.scope.exported_vars() {
            if !unset.contains(&name) {
                cmd.env(&name, value_to_string(&value));
            }
        }
    }

    // Remove unset variables
    for name in unset {
        cmd.env_remove(name);
    }

    // Apply overrides
    for (name, value) in overrides {
        cmd.env(name, value);
    }

    // Handle stdin
    if let Some(stdin_data) = ctx.read_stdin_to_string().await {
        cmd.stdin(std::process::Stdio::piped());
        cmd.stdout(std::process::Stdio::piped());
        cmd.stderr(std::process::Stdio::piped());

        let mut child = match cmd.spawn() {
            Ok(child) => child,
            Err(e) => return ExecResult::failure(127, format!("env: {}: {}", command, e)),
        };

        if let Some(mut stdin) = child.stdin.take() {
            use tokio::io::AsyncWriteExt;
            if let Err(e) = stdin.write_all(stdin_data.as_bytes()).await {
                return ExecResult::failure(1, format!("env: failed to write stdin: {}", e));
            }
        }

        match child.wait_with_output().await {
            Ok(output) => {
                let code = output.status.code().unwrap_or(-1) as i64;
                let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
                let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
                ExecResult::from_output(code, stdout, stderr)
            }
            Err(e) => ExecResult::failure(1, format!("env: failed to wait: {}", e)),
        }
    } else {
        cmd.stdout(std::process::Stdio::piped());
        cmd.stderr(std::process::Stdio::piped());

        match cmd.output().await {
            Ok(output) => {
                let code = output.status.code().unwrap_or(-1) as i64;
                let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
                let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
                ExecResult::from_output(code, stdout, stderr)
            }
            Err(e) => ExecResult::failure(127, format!("env: {}: {}", command, e)),
        }
    }
}

/// Convert a Value to a string.
fn value_to_string(value: &Value) -> String {
    match value {
        Value::Null => String::new(),
        Value::Bool(b) => b.to_string(),
        Value::Int(i) => i.to_string(),
        Value::Float(f) => f.to_string(),
        Value::String(s) => s.clone(),
        Value::Json(json) => json.to_string(),
        Value::Blob(blob) => format!("[blob: {} {}]", blob.formatted_size(), blob.content_type),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vfs::{MemoryFs, VfsRouter};
    use std::sync::Arc;

    fn make_ctx() -> ExecContext {
        let mut vfs = VfsRouter::new();
        vfs.mount("/", MemoryFs::new());
        ExecContext::new(Arc::new(vfs))
    }

    #[tokio::test]
    async fn test_env_prints_exports() {
        let mut ctx = make_ctx();
        ctx.scope.set_exported("PATH", Value::String("/usr/bin".into()));
        ctx.scope
            .set_exported("HOME", Value::String("/home/user".into()));

        let args = ToolArgs::new();
        let result = Env.execute(args, &mut ctx).await;

        assert!(result.ok());
        assert!(result.text_out().contains("PATH=/usr/bin"));
        assert!(result.text_out().contains("HOME=/home/user"));
    }

    #[tokio::test]
    async fn test_env_empty() {
        let mut ctx = make_ctx();
        let args = ToolArgs::new();

        let result = Env.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().is_empty());
    }

    #[cfg(feature = "native")]
    #[tokio::test]
    async fn test_env_with_command() {
        let mut ctx = make_ctx();

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("TEST_VAR=hello".into()));
        args.positional.push(Value::String("/bin/sh".into()));
        args.positional
            .push(Value::String("-c".into()));
        args.positional
            .push(Value::String("echo $TEST_VAR".into()));

        let result = Env.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert_eq!(result.text_out().trim(), "hello");
    }

    #[cfg(feature = "native")]
    #[tokio::test]
    async fn test_env_i_clears_environment() {
        let mut ctx = make_ctx();
        ctx.scope
            .set_exported("EXISTING", Value::String("value".into()));

        let mut args = ToolArgs::new();
        args.flags.insert("i".to_string());
        args.positional.push(Value::String("/usr/bin/env".into()));

        let result = Env.execute(args, &mut ctx).await;
        assert!(result.ok());
        // Should be empty or nearly empty (no EXISTING)
        assert!(!result.text_out().contains("EXISTING="));
    }

    #[tokio::test]
    async fn test_env_prints_overrides_without_command() {
        let mut ctx = make_ctx();
        ctx.scope.set_exported("A", Value::String("1".into()));

        let mut args = ToolArgs::new();
        args.positional.push(Value::String("B=2".into()));
        args.positional.push(Value::String("C=3".into()));

        let result = Env.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().contains("A=1"));
        assert!(result.text_out().contains("B=2"));
        assert!(result.text_out().contains("C=3"));
    }

    #[tokio::test]
    async fn test_env_null_separator() {
        let mut ctx = make_ctx();
        ctx.scope.set_exported("X", Value::String("1".into()));
        ctx.scope.set_exported("Y", Value::String("2".into()));

        let mut args = ToolArgs::new();
        args.flags.insert("0".to_string());

        let result = Env.execute(args, &mut ctx).await;
        assert!(result.ok());
        assert!(result.text_out().contains('\0'));
        assert!(!result.text_out().ends_with('\n'));
    }
}