Skip to main content

harn_cli/commands/
playground.rs

1use std::collections::HashSet;
2use std::io::{self, Write};
3use std::path::{Path, PathBuf};
4use std::sync::Arc;
5
6use harn_parser::{DiagnosticSeverity, Node, SNode, TypeChecker};
7
8use crate::cli::PlaygroundArgs;
9use crate::commands::run::{
10    connect_mcp_servers, install_cli_llm_mock_mode, persist_cli_llm_mock_recording, CliLlmMockMode,
11};
12use crate::package;
13use crate::skill_loader::{
14    emit_loader_warnings, install_skills_global, load_skills, SkillLoaderInputs,
15};
16
17#[derive(Clone, Debug, PartialEq, Eq)]
18struct LlmOverride {
19    provider: String,
20    model: String,
21}
22
23/// Inputs to `execute_playground_inputs` — the in-process sibling of
24/// `harn playground`. Tests construct this directly instead of going through
25/// clap. The binary entry (`run_command`) builds this from `PlaygroundArgs`.
26#[derive(Clone, Debug)]
27pub struct PlaygroundInputs {
28    pub host: PathBuf,
29    pub script: PathBuf,
30    pub task: String,
31    /// Optional `provider:model` override, in the same format `--llm` accepts.
32    pub llm: Option<String>,
33    pub llm_mock_mode: CliLlmMockMode,
34}
35
36#[derive(Clone, Debug)]
37struct PlaygroundConfig {
38    host: PathBuf,
39    script: PathBuf,
40    task: String,
41    llm: Option<LlmOverride>,
42    llm_mock_mode: CliLlmMockMode,
43}
44
45pub(crate) async fn run_command(
46    args: PlaygroundArgs,
47    llm_mock_mode: CliLlmMockMode,
48) -> Result<(), String> {
49    let config = PlaygroundConfig {
50        host: canonicalize_or_err(&args.host)?,
51        script: canonicalize_or_err(&args.script)?,
52        task: args.task.unwrap_or_default(),
53        llm: args.llm.as_deref().map(parse_llm_override).transpose()?,
54        llm_mock_mode,
55    };
56
57    if args.watch {
58        run_watch(&config).await
59    } else {
60        let output = execute_playground(&config).await?;
61        if !output.is_empty() {
62            io::stdout()
63                .write_all(output.as_bytes())
64                .map_err(|error| format!("failed to write playground output: {error}"))?;
65        }
66        Ok(())
67    }
68}
69
70/// In-process entry point for `harn playground`. Returns the captured stdout
71/// the CLI dispatcher would have printed, or a rendered error string.
72///
73/// This is the path tests use to exercise the playground driver without
74/// spawning the `harn` binary. Watch mode is not supported here — it has no
75/// terminal output contract worth asserting on.
76pub async fn execute_playground_inputs(inputs: PlaygroundInputs) -> Result<String, String> {
77    let llm = inputs.llm.as_deref().map(parse_llm_override).transpose()?;
78    let config = PlaygroundConfig {
79        host: canonicalize_or_err(inputs.host.to_string_lossy().as_ref())?,
80        script: canonicalize_or_err(inputs.script.to_string_lossy().as_ref())?,
81        task: inputs.task,
82        llm,
83        llm_mock_mode: inputs.llm_mock_mode,
84    };
85    execute_playground(&config).await
86}
87
88async fn run_watch(config: &PlaygroundConfig) -> Result<(), String> {
89    use notify::{Event, EventKind, RecursiveMode, Watcher};
90
91    eprintln!(
92        "\x1b[2m[playground] running {} with host {}...\x1b[0m",
93        config.script.display(),
94        config.host.display()
95    );
96    emit_run_result(execute_playground(config).await);
97
98    let roots = watch_roots(&config.host, &config.script);
99    let (tx, mut rx) = tokio::sync::mpsc::channel::<()>(1);
100    let _watcher = {
101        let tx = tx.clone();
102        let mut watcher = notify::recommended_watcher(move |res: Result<Event, _>| {
103            if let Ok(event) = res {
104                if matches!(
105                    event.kind,
106                    EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_)
107                ) {
108                    let has_harn = event
109                        .paths
110                        .iter()
111                        .any(|path| path.extension().is_some_and(|ext| ext == "harn"));
112                    if has_harn {
113                        let _ = tx.blocking_send(());
114                    }
115                }
116            }
117        })
118        .map_err(|error| format!("failed to create playground watcher: {error}"))?;
119
120        for root in &roots {
121            watcher
122                .watch(root, RecursiveMode::Recursive)
123                .map_err(|error| format!("failed to watch {}: {error}", root.display()))?;
124        }
125        watcher
126    };
127
128    eprintln!(
129        "\x1b[2m[playground] watching {} (ctrl-c to stop)\x1b[0m",
130        roots
131            .iter()
132            .map(|path| path.display().to_string())
133            .collect::<Vec<_>>()
134            .join(", ")
135    );
136
137    loop {
138        rx.recv().await;
139        tokio::time::sleep(std::time::Duration::from_millis(200)).await;
140        while rx.try_recv().is_ok() {}
141
142        eprintln!();
143        eprintln!(
144            "\x1b[2m[playground] change detected, re-running {}...\x1b[0m",
145            config.script.display()
146        );
147        emit_run_result(execute_playground(config).await);
148    }
149}
150
151fn emit_run_result(result: Result<String, String>) {
152    match result {
153        Ok(output) => {
154            if !output.is_empty() {
155                let _ = io::stdout().write_all(output.as_bytes());
156            }
157        }
158        Err(error) => eprint!("{error}"),
159    }
160}
161
162async fn execute_playground(config: &PlaygroundConfig) -> Result<String, String> {
163    let (host_source, host_program) = crate::parse_source_file(&config.host.to_string_lossy());
164    typecheck_program(&host_source, &host_program, &config.host, &HashSet::new())?;
165    let host_exports = exported_host_functions(&host_program);
166
167    let (script_source, script_program) =
168        crate::parse_source_file(&config.script.to_string_lossy());
169    typecheck_program(
170        &script_source,
171        &script_program,
172        &config.script,
173        &host_exports,
174    )?;
175
176    let chunk = harn_vm::Compiler::new()
177        .compile(&script_program)
178        .map_err(|error| format!("error: compile error: {error}\n"))?;
179
180    let env_guard = ScopedEnv::apply(config);
181    let source_parent = config
182        .script
183        .parent()
184        .unwrap_or_else(|| Path::new("."))
185        .to_path_buf();
186    let project_root = harn_vm::stdlib::process::find_project_root(&source_parent);
187    let store_base = project_root.as_deref().unwrap_or(source_parent.as_path());
188    let execution_cwd = std::env::current_dir()
189        .unwrap_or_else(|_| PathBuf::from("."))
190        .to_string_lossy()
191        .into_owned();
192    let source_dir = source_parent.to_string_lossy().into_owned();
193
194    let local = tokio::task::LocalSet::new();
195    let result = local
196        .run_until(async {
197            install_cli_llm_mock_mode(&config.llm_mock_mode)
198                .map_err(|error| format!("error: {error}\n"))?;
199            let host_vm = configured_vm(
200                &config.host,
201                &host_source,
202                project_root.as_deref(),
203                store_base,
204            )
205            .await?;
206            let bridge = Arc::new(
207                harn_vm::bridge::HostBridge::from_harn_module(host_vm, &config.host)
208                    .await
209                    .map_err(|error| format!("error: {error}\n"))?,
210            );
211
212            let mut vm = configured_vm(
213                &config.script,
214                &script_source,
215                project_root.as_deref(),
216                store_base,
217            )
218            .await?;
219            vm.set_bridge(bridge.clone());
220            harn_vm::llm::install_current_host_bridge(bridge.clone());
221            harn_vm::stdlib::process::set_thread_execution_context(Some(
222                harn_vm::orchestration::RunExecutionRecord {
223                    cwd: Some(execution_cwd),
224                    source_dir: Some(source_dir),
225                    env: std::collections::BTreeMap::new(),
226                    adapter: None,
227                    repo_path: None,
228                    worktree_path: None,
229                    branch: None,
230                    base_ref: None,
231                    cleanup: None,
232                },
233            ));
234            let execution_result = match vm.execute(&chunk).await {
235                Ok(_) => Ok(vm.output().to_string()),
236                Err(error) => Err(vm.format_runtime_error(&error)),
237            };
238            harn_vm::llm::clear_current_host_bridge();
239            harn_vm::stdlib::process::set_thread_execution_context(None);
240            persist_cli_llm_mock_recording(&config.llm_mock_mode)
241                .map_err(|error| format!("error: {error}\n"))?;
242            execution_result
243        })
244        .await;
245    drop(env_guard);
246    result
247}
248
249async fn configured_vm(
250    path: &Path,
251    source: &str,
252    project_root: Option<&Path>,
253    store_base: &Path,
254) -> Result<harn_vm::Vm, String> {
255    let mut vm = harn_vm::Vm::new();
256    harn_vm::register_vm_stdlib(&mut vm);
257    crate::install_default_hostlib(&mut vm);
258    harn_vm::register_store_builtins(&mut vm, store_base);
259    harn_vm::register_metadata_builtins(&mut vm, store_base);
260    let pipeline_name = path
261        .file_stem()
262        .and_then(|stem| stem.to_str())
263        .unwrap_or("default");
264    harn_vm::register_checkpoint_builtins(&mut vm, store_base, pipeline_name);
265    vm.set_source_info(&path.to_string_lossy(), source);
266    if let Some(root) = project_root {
267        vm.set_project_root(root);
268    }
269    if let Some(parent) = path.parent() {
270        if !parent.as_os_str().is_empty() {
271            vm.set_source_dir(parent);
272        }
273    }
274    vm.set_global(
275        "argv",
276        harn_vm::VmValue::List(std::sync::Arc::new(Vec::new())),
277    );
278    vm.set_harness(harn_vm::Harness::real());
279
280    let loaded = load_skills(&SkillLoaderInputs {
281        cli_dirs: Vec::new(),
282        source_path: Some(path.to_path_buf()),
283    });
284    emit_loader_warnings(&loaded.loader_warnings);
285    install_skills_global(&mut vm, &loaded);
286
287    let extensions = package::load_runtime_extensions(path);
288    package::install_runtime_extensions(&extensions);
289    if let Some(manifest) = extensions.root_manifest.as_ref() {
290        if !manifest.mcp.is_empty() {
291            connect_mcp_servers(&manifest.mcp, &mut vm).await;
292        }
293    }
294    package::install_manifest_triggers(&mut vm, &extensions)
295        .await
296        .map_err(|error| format!("failed to install manifest triggers: {error}"))?;
297
298    Ok(vm)
299}
300
301fn typecheck_program(
302    source: &str,
303    program: &[SNode],
304    path: &Path,
305    extra_names: &HashSet<String>,
306) -> Result<(), String> {
307    let graph = harn_modules::build(&[path.to_path_buf()]);
308    let mut checker = TypeChecker::new();
309    let mut imported = graph.imported_names_for_file(path).unwrap_or_default();
310    imported.extend(extra_names.iter().cloned());
311    if !imported.is_empty() {
312        checker = checker.with_imported_names(imported);
313    }
314    if let Some(imported) = graph.imported_type_declarations_for_file(path) {
315        checker = checker.with_imported_type_decls(imported);
316    }
317    if let Some(imported) = graph.imported_callable_declarations_for_file(path) {
318        checker = checker.with_imported_callable_decls(imported);
319    }
320
321    let diagnostics = checker.check(program);
322    let mut rendered = String::new();
323    let mut had_error = false;
324    for diagnostic in &diagnostics {
325        if diagnostic.severity == DiagnosticSeverity::Error {
326            had_error = true;
327        }
328        rendered.push_str(&harn_parser::diagnostic::render_type_diagnostic(
329            source,
330            &path.to_string_lossy(),
331            diagnostic,
332        ));
333    }
334
335    if had_error {
336        return Err(rendered);
337    }
338    if !rendered.is_empty() {
339        eprint!("{rendered}");
340    }
341    Ok(())
342}
343
344fn exported_host_functions(program: &[SNode]) -> HashSet<String> {
345    let mut public_names = HashSet::new();
346    let mut all_names = HashSet::new();
347    let mut has_pub_fn = false;
348
349    for node in program {
350        let inner = match &node.node {
351            Node::AttributedDecl { inner, .. } => inner.as_ref(),
352            _ => node,
353        };
354        let Node::FnDecl { name, is_pub, .. } = &inner.node else {
355            continue;
356        };
357        all_names.insert(name.clone());
358        if *is_pub {
359            has_pub_fn = true;
360            public_names.insert(name.clone());
361        }
362    }
363
364    if has_pub_fn {
365        public_names
366    } else {
367        all_names
368    }
369}
370
371fn watch_roots(host: &Path, script: &Path) -> Vec<PathBuf> {
372    let mut roots = Vec::new();
373    for candidate in [
374        host.parent().unwrap_or_else(|| Path::new(".")),
375        script.parent().unwrap_or_else(|| Path::new(".")),
376    ] {
377        if !roots.iter().any(|existing| existing == candidate) {
378            roots.push(candidate.to_path_buf());
379        }
380    }
381    roots
382}
383
384fn parse_llm_override(raw: &str) -> Result<LlmOverride, String> {
385    let (provider, model) = raw
386        .split_once(':')
387        .ok_or_else(|| "playground --llm expects provider:model".to_string())?;
388    let provider = provider.trim();
389    let model = model.trim();
390    if provider.is_empty() || model.is_empty() {
391        return Err("playground --llm expects provider:model".to_string());
392    }
393    Ok(LlmOverride {
394        provider: provider.to_string(),
395        model: model.to_string(),
396    })
397}
398
399fn canonicalize_or_err(path: &str) -> Result<PathBuf, String> {
400    std::fs::canonicalize(path).map_err(|error| format!("failed to resolve {path}: {error}"))
401}
402
403struct ScopedEnv {
404    previous: Vec<(String, Option<String>)>,
405}
406
407impl ScopedEnv {
408    fn apply(config: &PlaygroundConfig) -> Self {
409        let mut previous = Vec::new();
410        Self::set("HARN_TASK", Some(config.task.as_str()), &mut previous);
411        if let Some(llm) = &config.llm {
412            Self::set(
413                "HARN_LLM_PROVIDER",
414                Some(llm.provider.as_str()),
415                &mut previous,
416            );
417            Self::set("HARN_LLM_MODEL", Some(llm.model.as_str()), &mut previous);
418        }
419        Self { previous }
420    }
421
422    fn set(key: &str, value: Option<&str>, previous: &mut Vec<(String, Option<String>)>) {
423        previous.push((key.to_string(), std::env::var(key).ok()));
424        match value {
425            Some(value) => std::env::set_var(key, value),
426            None => std::env::remove_var(key),
427        }
428    }
429}
430
431impl Drop for ScopedEnv {
432    fn drop(&mut self) {
433        for (key, previous) in self.previous.iter().rev() {
434            match previous {
435                Some(value) => std::env::set_var(key, value),
436                None => std::env::remove_var(key),
437            }
438        }
439    }
440}
441
442#[cfg(test)]
443mod tests {
444    use super::*;
445
446    fn write_file(path: &Path, contents: &str) {
447        if let Some(parent) = path.parent() {
448            std::fs::create_dir_all(parent).unwrap();
449        }
450        std::fs::write(path, contents).unwrap();
451    }
452
453    #[test]
454    fn exported_host_functions_prefers_pub_names() {
455        let temp = tempfile::tempdir().unwrap();
456        let path = temp.path().join("host_pub.harn");
457        let source = r"
458fn helper() {}
459pub fn run_shell(command) { return command }
460pub fn request_permission(tool_name, request_args) { return true }
461";
462        write_file(&path, source);
463        let (_, program) = crate::parse_source_file(path.to_string_lossy().as_ref());
464        let names = exported_host_functions(&program);
465        assert!(names.contains("run_shell"));
466        assert!(names.contains("request_permission"));
467        assert!(!names.contains("helper"));
468    }
469
470    #[test]
471    fn parse_llm_override_splits_provider_and_model() {
472        let parsed = parse_llm_override("ollama:qwen2.5-coder:latest").unwrap();
473        assert_eq!(parsed.provider, "ollama");
474        assert_eq!(parsed.model, "qwen2.5-coder:latest");
475    }
476
477    #[tokio::test(flavor = "current_thread")]
478    async fn playground_executes_host_backed_script() {
479        let _guard = crate::tests::common::env_lock::lock_env().lock().await;
480        let temp = tempfile::tempdir().unwrap();
481        let host = temp.path().join("host.harn");
482        let script = temp.path().join("pipeline.harn");
483        write_file(
484            &host,
485            r#"
486pub fn build_prompt(task) {
487  return "prompt: " + task
488}
489"#,
490        );
491        write_file(
492            &script,
493            r#"
494pipeline default(task) {
495  llm_mock({text: "done"})
496  let result = llm_call(build_prompt(env_or("HARN_TASK", "")), "You are concise.")
497  __io_println(result.text)
498}
499"#,
500        );
501
502        let output = execute_playground(&PlaygroundConfig {
503            host,
504            script,
505            task: "ship it".to_string(),
506            llm: Some(LlmOverride {
507                provider: "mock".to_string(),
508                model: "mock".to_string(),
509            }),
510            llm_mock_mode: CliLlmMockMode::Off,
511        })
512        .await
513        .unwrap();
514
515        assert!(output.contains("done"));
516    }
517
518    #[tokio::test(flavor = "current_thread")]
519    async fn playground_reports_missing_capability_with_caller_context() {
520        let _guard = crate::tests::common::env_lock::lock_env().lock().await;
521        let temp = tempfile::tempdir().unwrap();
522        let host = temp.path().join("host.harn");
523        let script = temp.path().join("pipeline.harn");
524        write_file(
525            &host,
526            r#"
527pub fn helper() {
528  return "ok"
529}
530"#,
531        );
532        write_file(
533            &script,
534            r#"
535pipeline default(task) {
536  run_shell("pwd")
537}
538"#,
539        );
540
541        let error = execute_playground(&PlaygroundConfig {
542            host,
543            script,
544            task: String::new(),
545            llm: None,
546            llm_mock_mode: CliLlmMockMode::Off,
547        })
548        .await
549        .unwrap_err();
550
551        assert!(error.contains("run_shell"));
552        assert!(error.contains("pipeline.harn:3:3"));
553    }
554
555    #[tokio::test(flavor = "current_thread")]
556    async fn playground_replays_cli_llm_mock_fixtures() {
557        let _guard = crate::tests::common::env_lock::lock_env().lock().await;
558        let temp = tempfile::tempdir().unwrap();
559        let host = temp.path().join("host.harn");
560        let script = temp.path().join("pipeline.harn");
561        let fixtures = temp.path().join("fixtures.jsonl");
562        write_file(
563            &host,
564            r#"
565pub fn build_prompt(task) {
566  return "prompt: " + task
567}
568"#,
569        );
570        write_file(
571            &script,
572            r#"
573pipeline default(task) {
574  let result = llm_call(build_prompt(env_or("HARN_TASK", "")), "You are concise.")
575  __io_println(result.text)
576}
577"#,
578        );
579        write_file(
580            &fixtures,
581            r#"{"text":"fixture replay","model":"fixture-model"}
582"#,
583        );
584
585        let output = execute_playground(&PlaygroundConfig {
586            host,
587            script,
588            task: "ship it".to_string(),
589            llm: Some(LlmOverride {
590                provider: "anthropic".to_string(),
591                model: "claude-sonnet".to_string(),
592            }),
593            llm_mock_mode: CliLlmMockMode::Replay {
594                fixture_path: fixtures,
595            },
596        })
597        .await
598        .unwrap();
599
600        assert!(output.contains("fixture replay"));
601    }
602
603    #[tokio::test(flavor = "current_thread")]
604    async fn playground_replays_cli_llm_mock_error_envelopes() {
605        let _guard = crate::tests::common::env_lock::lock_env().lock().await;
606        let temp = tempfile::tempdir().unwrap();
607        let host = temp.path().join("host.harn");
608        let script = temp.path().join("pipeline.harn");
609        let fixtures = temp.path().join("fixtures.jsonl");
610        write_file(&host, "");
611        write_file(
612            &script,
613            r#"
614pipeline default(task) {
615  let first = llm_call_safe("first", nil, {provider: "mock", model: "mock-model"})
616  __io_println(first.ok)
617  __io_println(first.error.status)
618  __io_println(first.error.kind)
619  __io_println(first.error.reason)
620  let second = llm_call("second", nil, {provider: "mock", model: "mock-model"})
621  __io_println(second.text)
622}
623"#,
624        );
625        write_file(
626            &fixtures,
627            r#"{"error":{"status":503,"kind":"transient","reason":"upstream_unavailable"}}
628{"text":"recovered","tool_calls":[]}
629"#,
630        );
631
632        let output = execute_playground(&PlaygroundConfig {
633            host,
634            script,
635            task: String::new(),
636            llm: None,
637            llm_mock_mode: CliLlmMockMode::Replay {
638                fixture_path: fixtures,
639            },
640        })
641        .await
642        .unwrap();
643
644        assert!(output.contains("false\n503\ntransient\nupstream_unavailable\nrecovered"));
645    }
646}