assay-lua 0.18.10

General-purpose enhanced Lua runtime. Batteries-included scripting, automation, and web services.
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
mod api;
mod checks;
mod cli;
mod config;
mod lua;
mod mcp;
mod output;
mod runner;
mod tool_mode;

use std::process::ExitCode;
use std::time::Duration;

use assay::install;
use clap::Parser;
use tracing::{error, info};
use tracing_subscriber::EnvFilter;

use cli::args::{Cli, Commands};
use tool_mode::{format_lua_error, resume_tool_execution, run_lua_tool_mode};

const DEFAULT_TOOL_TIMEOUT_SECS: u64 = 20;
const DEFAULT_RESUME_TTL_SECS: u64 = 3600;
const TOOL_STDOUT_CAP_BYTES: usize = 512 * 1024;
const APPROVAL_REQUEST_PREFIX: &str = "__assay_approval_request__:";

pub fn build_http_client() -> reqwest::Client {
    reqwest::Client::builder()
        .timeout(Duration::from_secs(30))
        .build()
        .expect("building HTTP client")
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ScriptMode {
    Script,
    Tool,
}

#[derive(Clone, Debug)]
struct RunOptions {
    mode: ScriptMode,
    timeout_secs: u64,
    exec_mode: lua::ExecMode,
    approval: lua::ApprovalConfig,
}

impl Default for RunOptions {
    fn default() -> Self {
        Self {
            mode: resolve_script_mode(None),
            timeout_secs: DEFAULT_TOOL_TIMEOUT_SECS,
            exec_mode: resolve_exec_mode(false, false),
            approval: lua::approval_config_from_env(),
        }
    }
}

/// Resolve the execution mode from CLI flags and environment. Approval
/// mode wins over read-only when both are requested.
fn resolve_exec_mode(cli_readonly: bool, cli_approval: bool) -> lua::ExecMode {
    if cli_approval || lua::approval_from_env() {
        lua::ExecMode::Approval
    } else if cli_readonly || lua::readonly_from_env() {
        lua::ExecMode::ReadOnly
    } else {
        lua::ExecMode::Unrestricted
    }
}

#[tokio::main]
async fn main() -> ExitCode {
    let cli = Cli::parse();
    let exec_mode = resolve_exec_mode(cli.readonly, cli.approval_mode);
    let readonly = exec_mode.is_readonly();
    let approval = if exec_mode.is_approval() {
        lua::approval_config_from_env()
    } else {
        lua::ApprovalConfig::default()
    };

    let filter = if cli.verbose {
        EnvFilter::new("debug")
    } else {
        EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"))
    };

    tracing_subscriber::fmt()
        .with_env_filter(filter)
        .with_target(false)
        .with_writer(std::io::stderr)
        .init();

    match cli.command {
        Some(Commands::Context { query, limit }) => run_context(&query, limit),
        Some(Commands::Exec { eval, file }) => {
            if let Some(code) = eval {
                run_lua_inline(&code, exec_mode, &approval).await
            } else if let Some(path) = file {
                let options = RunOptions {
                    exec_mode,
                    approval: approval.clone(),
                    ..RunOptions::default()
                };
                run_lua_script(&path, options, Vec::new()).await
            } else {
                eprintln!("error: exec requires either -e <code> or a file path");
                ExitCode::from(1)
            }
        }
        Some(Commands::Modules) => run_modules(exec_mode),
        Some(Commands::Run {
            file,
            mode,
            timeout,
            script_args,
        }) => {
            let options = RunOptions {
                mode: resolve_script_mode(mode.as_deref()),
                timeout_secs: timeout.unwrap_or(DEFAULT_TOOL_TIMEOUT_SECS),
                exec_mode,
                approval: approval.clone(),
            };
            dispatch_file(&file, options, script_args).await
        }
        Some(Commands::Resume {
            token,
            approve,
            resume_ttl,
            approver,
        }) => {
            resume_tool_execution(&token, &approve, resume_ttl, readonly, approver.as_deref()).await
        }
        Some(Commands::Workflow { global, command }) => {
            cli::dispatch::workflow(global, command).await
        }
        Some(Commands::Schedule { global, command }) => {
            cli::dispatch::schedule(global, command).await
        }
        Some(Commands::Namespace { global, command }) => {
            cli::dispatch::namespace(global, command).await
        }
        Some(Commands::Worker { global, command }) => cli::dispatch::worker(global, command).await,
        Some(Commands::Queue { global, command }) => cli::dispatch::queue(global, command).await,
        Some(Commands::Install(args)) => install::run(args).await,
        Some(Commands::McpServe) => mcp::serve().await,
        Some(Commands::ApiServe { bind }) => api::serve(&bind).await,
        Some(Commands::Completion { shell }) => {
            use clap::CommandFactory;
            let mut cmd = Cli::command();
            // Buffer first so we can exit cleanly on a broken pipe
            // (e.g. `assay completion bash | head`): clap_complete's
            // default writer panics on BrokenPipe.
            let mut buf: Vec<u8> = Vec::new();
            clap_complete::generate(shell, &mut cmd, "assay", &mut buf);
            use std::io::Write;
            match std::io::stdout().write_all(&buf) {
                Ok(()) => ExitCode::SUCCESS,
                Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => ExitCode::SUCCESS,
                Err(e) => {
                    eprintln!("error: writing completion: {e}");
                    ExitCode::from(1)
                }
            }
        }
        None => {
            if let Some(ref file) = cli.file {
                let options = RunOptions {
                    exec_mode,
                    approval: approval.clone(),
                    ..RunOptions::default()
                };
                dispatch_file(file, options, Vec::new()).await
            } else {
                use clap::CommandFactory;
                Cli::command().print_help().ok();
                println!();
                ExitCode::from(1)
            }
        }
    }
}

fn resolve_script_mode(cli_mode: Option<&str>) -> ScriptMode {
    match cli_mode
        .map(std::borrow::ToOwned::to_owned)
        .or_else(|| std::env::var("ASSAY_MODE").ok())
        .as_deref()
    {
        Some("tool") => ScriptMode::Tool,
        _ => ScriptMode::Script,
    }
}

async fn dispatch_file(
    file: &std::path::Path,
    options: RunOptions,
    script_args: Vec<String>,
) -> ExitCode {
    let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");

    match ext {
        "yaml" | "yml" => run_yaml_checks(file, options.exec_mode).await,
        "lua" => run_lua_script(file, options, script_args).await,
        other => {
            eprintln!(
                "error: unsupported file extension {other:?} (expected .yaml, .yml, or .lua)"
            );
            ExitCode::from(1)
        }
    }
}

async fn run_yaml_checks(path: &std::path::Path, exec_mode: lua::ExecMode) -> ExitCode {
    let readonly = exec_mode.is_readonly();
    info!(config = %path.display(), readonly, "starting assay (check mode)");

    let cfg = match config::load(path) {
        Ok(cfg) => cfg,
        Err(e) => {
            eprintln!("error: loading config from {}: {e:#}", path.display());
            return ExitCode::from(1);
        }
    };

    info!(
        checks = cfg.checks.len(),
        timeout_secs = cfg.timeout.as_secs(),
        retries = cfg.retries,
        "configuration loaded"
    );

    let result = runner::run(&cfg, exec_mode).await;
    result.print()
}

async fn run_lua_script(
    path: &std::path::Path,
    options: RunOptions,
    script_args: Vec<String>,
) -> ExitCode {
    let script = match std::fs::read_to_string(path) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("error: reading {}: {e}", path.display());
            return ExitCode::from(1);
        }
    };

    let script = lua::async_bridge::strip_shebang(&script);

    match options.mode {
        ScriptMode::Script => {
            run_lua_script_mode(
                path,
                script,
                script_args,
                options.exec_mode,
                &options.approval,
            )
            .await
        }
        ScriptMode::Tool => {
            run_lua_tool_mode(tool_mode::ToolModeRequest {
                path,
                script,
                timeout_secs: options.timeout_secs,
                script_args,
                exec_mode: options.exec_mode,
                approval: &options.approval,
            })
            .await
        }
    }
}

/// Populate Lua's standard `arg` global from the trailing positional
/// arguments we collected in `Commands::Run`. Mirrors stock `lua`:
/// `arg[0]` is the script path, `arg[1..]` are the user-passed args.
fn install_script_args(
    vm: &mlua::Lua,
    path: &std::path::Path,
    script_args: &[String],
) -> mlua::Result<()> {
    let table = vm.create_table()?;
    table.set(0, path.display().to_string())?;
    for (i, a) in script_args.iter().enumerate() {
        table.set(i as i64 + 1, a.as_str())?;
    }
    vm.globals().set("arg", table)
}

async fn run_lua_script_mode(
    path: &std::path::Path,
    script: &str,
    script_args: Vec<String>,
    exec_mode: lua::ExecMode,
    approval: &lua::ApprovalConfig,
) -> ExitCode {
    let readonly = exec_mode.is_readonly();
    info!(script = %path.display(), readonly, "starting assay (script mode)");

    let client = build_http_client();

    let vm = match lua::create_vm_with_options(
        client,
        lua::VmOptions {
            global_modules_path: None,
            mode: exec_mode,
            approval: approval.clone(),
        },
    ) {
        Ok(vm) => vm,
        Err(e) => {
            eprintln!("error: creating Lua VM: {e:#}");
            return ExitCode::from(1);
        }
    };

    if let Err(e) = install_script_args(&vm, path, &script_args) {
        eprintln!("error: installing arg global: {e}");
        return ExitCode::from(1);
    }

    let local = tokio::task::LocalSet::new();
    let result = local
        .run_until(async {
            vm.load(script)
                .set_name(format!("@{}", path.display()))
                .exec_async()
                .await
        })
        .await;

    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            error!("{}", format_lua_error(&e));
            ExitCode::from(1)
        }
    }
}

async fn run_lua_inline(
    code: &str,
    exec_mode: lua::ExecMode,
    approval: &lua::ApprovalConfig,
) -> ExitCode {
    let readonly = exec_mode.is_readonly();
    info!(readonly, "starting assay (inline eval mode)");

    let client = build_http_client();

    let vm = match lua::create_vm_with_options(
        client,
        lua::VmOptions {
            global_modules_path: None,
            mode: exec_mode,
            approval: approval.clone(),
        },
    ) {
        Ok(vm) => vm,
        Err(e) => {
            eprintln!("error: creating Lua VM: {e:#}");
            return ExitCode::from(1);
        }
    };

    let script = lua::async_bridge::strip_shebang(code);

    let local = tokio::task::LocalSet::new();
    let result = local
        .run_until(async { vm.load(script).set_name("@<eval>").exec_async().await })
        .await;

    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            error!("{}", format_lua_error(&e));
            ExitCode::from(1)
        }
    }
}
fn run_modules(exec_mode: lua::ExecMode) -> ExitCode {
    use assay::discovery::{ModuleSource, discover_modules};

    let modules = discover_modules();

    // Deduplicate by name (Project > Global > BuiltIn priority already in order)
    let mut seen = std::collections::HashSet::new();
    let mut unique: Vec<_> = modules
        .into_iter()
        .filter(|m| seen.insert(m.module_name.clone()))
        .collect();

    // Sort alphabetically for consistent output
    unique.sort_by(|a, b| a.module_name.cmp(&b.module_name));

    // Print header
    println!("{:<30} {:<10} DESCRIPTION", "MODULE", "SOURCE");
    println!("{}", "-".repeat(80));

    for m in &unique {
        let source_label = match m.source {
            ModuleSource::BuiltIn => "builtin",
            ModuleSource::Project => "project",
            ModuleSource::Global => "global",
        };
        println!(
            "{:<30} {:<10} {}",
            m.module_name, source_label, m.metadata.description
        );
    }

    if exec_mode.is_readonly() {
        println!();
        println!(
            "read-only mode active: mutating builtins raise 'readonly: <name> blocked' errors"
        );
    } else if exec_mode.is_approval() {
        println!();
        println!("approval mode active: mutating builtins pause for per-operation approval");
    }

    ExitCode::SUCCESS
}

fn run_context(query: &str, limit: usize) -> ExitCode {
    match render_context(query, limit) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(e) => {
            eprintln!("error: {e}");
            ExitCode::from(1)
        }
    }
}

/// Render prompt-ready module context Markdown for `query`, returning the
/// same text the `assay context` CLI prints. Shared by the CLI path and
/// the MCP `assay_context` tool.
fn render_context(query: &str, limit: usize) -> Result<String, String> {
    use assay::context::{ModuleContextEntry, QuickRefEntry, format_context};
    use assay::discovery::{discover_modules, search_modules};

    // Run on a dedicated thread to avoid tokio runtime nesting.
    // FTS5Index creates its own tokio::Runtime for SQLite operations,
    // which panics if called from within the #[tokio::main] context.
    let query = query.to_string();
    let handle = std::thread::spawn(move || {
        let results = search_modules(&query, limit);
        let all_modules = discover_modules();

        let entries: Vec<ModuleContextEntry> = results
            .iter()
            .filter_map(|result| {
                all_modules
                    .iter()
                    .find(|m| m.module_name == result.id)
                    .map(|m| ModuleContextEntry {
                        module_name: m.module_name.clone(),
                        description: m.metadata.description.clone(),
                        env_vars: m.metadata.env_vars.clone(),
                        quickrefs: m
                            .metadata
                            .quickrefs
                            .iter()
                            .map(|qr| QuickRefEntry {
                                signature: qr.signature.clone(),
                                return_hint: qr.return_hint.clone(),
                                description: qr.description.clone(),
                            })
                            .collect(),
                    })
            })
            .collect();

        format_context(&entries)
    });

    handle
        .join()
        .map_err(|_| "context search failed".to_string())
}