harn-cli 0.7.20

CLI for the Harn programming language — run, test, REPL, format, and lint
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
501
502
503
504
505
use std::collections::HashSet;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::rc::Rc;

use harn_parser::{DiagnosticSeverity, Node, SNode, TypeChecker};

use crate::cli::PlaygroundArgs;
use crate::commands::run::connect_mcp_servers;
use crate::package;
use crate::skill_loader::{
    emit_loader_warnings, install_skills_global, load_skills, SkillLoaderInputs,
};

#[derive(Clone, Debug, PartialEq, Eq)]
struct LlmOverride {
    provider: String,
    model: String,
}

#[derive(Clone, Debug)]
struct PlaygroundConfig {
    host: PathBuf,
    script: PathBuf,
    task: String,
    llm: Option<LlmOverride>,
}

pub(crate) async fn run_command(args: PlaygroundArgs) -> Result<(), String> {
    let config = PlaygroundConfig {
        host: canonicalize_or_err(&args.host)?,
        script: canonicalize_or_err(&args.script)?,
        task: args.task.unwrap_or_default(),
        llm: args.llm.as_deref().map(parse_llm_override).transpose()?,
    };

    if args.watch {
        run_watch(&config).await
    } else {
        let output = execute_playground(&config).await?;
        if !output.is_empty() {
            io::stdout()
                .write_all(output.as_bytes())
                .map_err(|error| format!("failed to write playground output: {error}"))?;
        }
        Ok(())
    }
}

async fn run_watch(config: &PlaygroundConfig) -> Result<(), String> {
    use notify::{Event, EventKind, RecursiveMode, Watcher};

    eprintln!(
        "\x1b[2m[playground] running {} with host {}...\x1b[0m",
        config.script.display(),
        config.host.display()
    );
    emit_run_result(execute_playground(config).await);

    let roots = watch_roots(&config.host, &config.script);
    let (tx, mut rx) = tokio::sync::mpsc::channel::<()>(1);
    let _watcher = {
        let tx = tx.clone();
        let mut watcher = notify::recommended_watcher(move |res: Result<Event, _>| {
            if let Ok(event) = res {
                if matches!(
                    event.kind,
                    EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_)
                ) {
                    let has_harn = event
                        .paths
                        .iter()
                        .any(|path| path.extension().is_some_and(|ext| ext == "harn"));
                    if has_harn {
                        let _ = tx.blocking_send(());
                    }
                }
            }
        })
        .map_err(|error| format!("failed to create playground watcher: {error}"))?;

        for root in &roots {
            watcher
                .watch(root, RecursiveMode::Recursive)
                .map_err(|error| format!("failed to watch {}: {error}", root.display()))?;
        }
        watcher
    };

    eprintln!(
        "\x1b[2m[playground] watching {} (ctrl-c to stop)\x1b[0m",
        roots
            .iter()
            .map(|path| path.display().to_string())
            .collect::<Vec<_>>()
            .join(", ")
    );

    loop {
        rx.recv().await;
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;
        while rx.try_recv().is_ok() {}

        eprintln!();
        eprintln!(
            "\x1b[2m[playground] change detected, re-running {}...\x1b[0m",
            config.script.display()
        );
        emit_run_result(execute_playground(config).await);
    }
}

fn emit_run_result(result: Result<String, String>) {
    match result {
        Ok(output) => {
            if !output.is_empty() {
                let _ = io::stdout().write_all(output.as_bytes());
            }
        }
        Err(error) => eprint!("{error}"),
    }
}

async fn execute_playground(config: &PlaygroundConfig) -> Result<String, String> {
    let (host_source, host_program) = crate::parse_source_file(&config.host.to_string_lossy());
    typecheck_program(&host_source, &host_program, &config.host, &HashSet::new())?;
    let host_exports = exported_host_functions(&host_program);

    let (script_source, script_program) =
        crate::parse_source_file(&config.script.to_string_lossy());
    typecheck_program(
        &script_source,
        &script_program,
        &config.script,
        &host_exports,
    )?;

    let chunk = harn_vm::Compiler::new()
        .compile(&script_program)
        .map_err(|error| format!("error: compile error: {error}\n"))?;

    let env_guard = ScopedEnv::apply(config);
    let source_parent = config
        .script
        .parent()
        .unwrap_or_else(|| Path::new("."))
        .to_path_buf();
    let project_root = harn_vm::stdlib::process::find_project_root(&source_parent);
    let store_base = project_root.as_deref().unwrap_or(source_parent.as_path());
    let execution_cwd = std::env::current_dir()
        .unwrap_or_else(|_| PathBuf::from("."))
        .to_string_lossy()
        .into_owned();
    let source_dir = source_parent.to_string_lossy().into_owned();

    let local = tokio::task::LocalSet::new();
    let result = local
        .run_until(async {
            let host_vm = configured_vm(
                &config.host,
                &host_source,
                project_root.as_deref(),
                store_base,
            )
            .await?;
            let bridge = Rc::new(
                harn_vm::bridge::HostBridge::from_harn_module(host_vm, &config.host)
                    .await
                    .map_err(|error| format!("error: {error}\n"))?,
            );

            let mut vm = configured_vm(
                &config.script,
                &script_source,
                project_root.as_deref(),
                store_base,
            )
            .await?;
            vm.set_bridge(bridge.clone());
            harn_vm::llm::register_agent_loop_with_bridge(&mut vm, bridge.clone());
            harn_vm::llm::register_llm_call_with_bridge(&mut vm, bridge);
            harn_vm::stdlib::process::set_thread_execution_context(Some(
                harn_vm::orchestration::RunExecutionRecord {
                    cwd: Some(execution_cwd),
                    source_dir: Some(source_dir),
                    env: std::collections::BTreeMap::new(),
                    adapter: None,
                    repo_path: None,
                    worktree_path: None,
                    branch: None,
                    base_ref: None,
                    cleanup: None,
                },
            ));
            let execution_result = match vm.execute(&chunk).await {
                Ok(_) => Ok(vm.output().to_string()),
                Err(error) => Err(vm.format_runtime_error(&error)),
            };
            harn_vm::stdlib::process::set_thread_execution_context(None);
            execution_result
        })
        .await;
    drop(env_guard);
    result
}

async fn configured_vm(
    path: &Path,
    source: &str,
    project_root: Option<&Path>,
    store_base: &Path,
) -> Result<harn_vm::Vm, String> {
    let mut vm = harn_vm::Vm::new();
    harn_vm::register_vm_stdlib(&mut vm);
    harn_vm::register_store_builtins(&mut vm, store_base);
    harn_vm::register_metadata_builtins(&mut vm, store_base);
    let pipeline_name = path
        .file_stem()
        .and_then(|stem| stem.to_str())
        .unwrap_or("default");
    harn_vm::register_checkpoint_builtins(&mut vm, store_base, pipeline_name);
    vm.set_source_info(&path.to_string_lossy(), source);
    if let Some(root) = project_root {
        vm.set_project_root(root);
    }
    if let Some(parent) = path.parent() {
        if !parent.as_os_str().is_empty() {
            vm.set_source_dir(parent);
        }
    }
    vm.set_global("argv", harn_vm::VmValue::List(Rc::new(Vec::new())));

    let loaded = load_skills(&SkillLoaderInputs {
        cli_dirs: Vec::new(),
        source_path: Some(path.to_path_buf()),
    });
    emit_loader_warnings(&loaded.loader_warnings);
    install_skills_global(&mut vm, &loaded);

    if let Some(manifest) = package::try_read_manifest_for(path) {
        package::install_capability_overrides(&manifest);
        if !manifest.mcp.is_empty() {
            connect_mcp_servers(&manifest.mcp, &mut vm).await;
        }
    }

    Ok(vm)
}

fn typecheck_program(
    source: &str,
    program: &[SNode],
    path: &Path,
    extra_names: &HashSet<String>,
) -> Result<(), String> {
    let graph = harn_modules::build(&[path.to_path_buf()]);
    let mut checker = TypeChecker::new();
    let mut imported = graph.imported_names_for_file(path).unwrap_or_default();
    imported.extend(extra_names.iter().cloned());
    if !imported.is_empty() {
        checker = checker.with_imported_names(imported);
    }

    let diagnostics = checker.check(program);
    let mut rendered = String::new();
    let mut had_error = false;
    for diagnostic in &diagnostics {
        let severity = match diagnostic.severity {
            DiagnosticSeverity::Error => {
                had_error = true;
                "error"
            }
            DiagnosticSeverity::Warning => "warning",
        };
        if let Some(span) = &diagnostic.span {
            rendered.push_str(&harn_parser::diagnostic::render_diagnostic(
                source,
                &path.to_string_lossy(),
                span,
                severity,
                &diagnostic.message,
                None,
                diagnostic.help.as_deref(),
            ));
        } else {
            rendered.push_str(&format!("{severity}: {}\n", diagnostic.message));
        }
    }

    if had_error {
        return Err(rendered);
    }
    if !rendered.is_empty() {
        eprint!("{rendered}");
    }
    Ok(())
}

fn exported_host_functions(program: &[SNode]) -> HashSet<String> {
    let mut public_names = HashSet::new();
    let mut all_names = HashSet::new();
    let mut has_pub_fn = false;

    for node in program {
        let inner = match &node.node {
            Node::AttributedDecl { inner, .. } => inner.as_ref(),
            _ => node,
        };
        let Node::FnDecl { name, is_pub, .. } = &inner.node else {
            continue;
        };
        all_names.insert(name.clone());
        if *is_pub {
            has_pub_fn = true;
            public_names.insert(name.clone());
        }
    }

    if has_pub_fn {
        public_names
    } else {
        all_names
    }
}

fn watch_roots(host: &Path, script: &Path) -> Vec<PathBuf> {
    let mut roots = Vec::new();
    for candidate in [
        host.parent().unwrap_or_else(|| Path::new(".")),
        script.parent().unwrap_or_else(|| Path::new(".")),
    ] {
        if !roots.iter().any(|existing| existing == candidate) {
            roots.push(candidate.to_path_buf());
        }
    }
    roots
}

fn parse_llm_override(raw: &str) -> Result<LlmOverride, String> {
    let (provider, model) = raw
        .split_once(':')
        .ok_or_else(|| "playground --llm expects provider:model".to_string())?;
    let provider = provider.trim();
    let model = model.trim();
    if provider.is_empty() || model.is_empty() {
        return Err("playground --llm expects provider:model".to_string());
    }
    Ok(LlmOverride {
        provider: provider.to_string(),
        model: model.to_string(),
    })
}

fn canonicalize_or_err(path: &str) -> Result<PathBuf, String> {
    std::fs::canonicalize(path).map_err(|error| format!("failed to resolve {path}: {error}"))
}

struct ScopedEnv {
    previous: Vec<(String, Option<String>)>,
}

impl ScopedEnv {
    fn apply(config: &PlaygroundConfig) -> Self {
        let mut previous = Vec::new();
        Self::set("HARN_TASK", Some(config.task.as_str()), &mut previous);
        if let Some(llm) = &config.llm {
            Self::set(
                "HARN_LLM_PROVIDER",
                Some(llm.provider.as_str()),
                &mut previous,
            );
            Self::set("HARN_LLM_MODEL", Some(llm.model.as_str()), &mut previous);
        }
        Self { previous }
    }

    fn set(key: &str, value: Option<&str>, previous: &mut Vec<(String, Option<String>)>) {
        previous.push((key.to_string(), std::env::var(key).ok()));
        match value {
            Some(value) => std::env::set_var(key, value),
            None => std::env::remove_var(key),
        }
    }
}

impl Drop for ScopedEnv {
    fn drop(&mut self) {
        for (key, previous) in self.previous.iter().rev() {
            match previous {
                Some(value) => std::env::set_var(key, value),
                None => std::env::remove_var(key),
            }
        }
    }
}

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

    fn write_file(path: &Path, contents: &str) {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(path, contents).unwrap();
    }

    #[test]
    fn exported_host_functions_prefers_pub_names() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("host_pub.harn");
        let source = r#"
fn helper() {}
pub fn run_shell(command) { return command }
pub fn request_permission(tool_name, request_args) { return true }
"#;
        write_file(&path, source);
        let (_, program) = crate::parse_source_file(path.to_string_lossy().as_ref());
        let names = exported_host_functions(&program);
        assert!(names.contains("run_shell"));
        assert!(names.contains("request_permission"));
        assert!(!names.contains("helper"));
    }

    #[test]
    fn parse_llm_override_splits_provider_and_model() {
        let parsed = parse_llm_override("ollama:qwen2.5-coder:latest").unwrap();
        assert_eq!(parsed.provider, "ollama");
        assert_eq!(parsed.model, "qwen2.5-coder:latest");
    }

    #[tokio::test(flavor = "current_thread")]
    async fn playground_executes_host_backed_script() {
        let temp = tempfile::tempdir().unwrap();
        let host = temp.path().join("host.harn");
        let script = temp.path().join("pipeline.harn");
        write_file(
            &host,
            r#"
pub fn build_prompt(task) {
  return "prompt: " + task
}
"#,
        );
        write_file(
            &script,
            r#"
pipeline default(task) {
  llm_mock({text: "done"})
  let result = llm_call(build_prompt(env_or("HARN_TASK", "")), "You are concise.")
  println(result.text)
}
"#,
        );

        let output = execute_playground(&PlaygroundConfig {
            host,
            script,
            task: "ship it".to_string(),
            llm: Some(LlmOverride {
                provider: "mock".to_string(),
                model: "mock".to_string(),
            }),
        })
        .await
        .unwrap();

        assert!(output.contains("done"));
    }

    #[tokio::test(flavor = "current_thread")]
    async fn playground_reports_missing_capability_with_caller_context() {
        let temp = tempfile::tempdir().unwrap();
        let host = temp.path().join("host.harn");
        let script = temp.path().join("pipeline.harn");
        write_file(
            &host,
            r#"
pub fn helper() {
  return "ok"
}
"#,
        );
        write_file(
            &script,
            r#"
pipeline default(task) {
  run_shell("pwd")
}
"#,
        );

        let error = execute_playground(&PlaygroundConfig {
            host,
            script,
            task: String::new(),
            llm: None,
        })
        .await
        .unwrap_err();

        assert!(error.contains("run_shell"));
        assert!(error.contains("pipeline.harn:3:3"));
    }
}