Skip to main content

harn_vm/vm/
state.rs

1use std::collections::{BTreeMap, HashMap, HashSet};
2use std::path::PathBuf;
3use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
4use std::sync::Arc;
5use std::time::Instant;
6
7use crate::chunk::{Chunk, ChunkRef, Constant};
8use crate::runtime_limits::RuntimeLimits;
9use crate::value::{
10    ModuleFunctionRegistry, VmAsyncBuiltinFn, VmBuiltinFn, VmClosure, VmEnv, VmError, VmMutex,
11    VmTaskHandle, VmValue,
12};
13use crate::BuiltinId;
14
15use super::debug::DebugHook;
16use super::modules::ModuleCache;
17use super::VmBuiltinMetadata;
18
19/// A lazy callable's resolved export set together with the module graph that
20/// was loaded to produce it.
21///
22/// The exported closures — and every function they can transitively reach
23/// through imports — hold only `Weak`s into their home module's function
24/// registry and module state. The child VM that first loaded the graph is
25/// normally their sole strong owner (via its `module_cache`), and it dies once
26/// the hook fire completes. A later child VM that hits this cache never
27/// re-imports the graph, so a transitively imported callee's sibling `pub fn`
28/// would fall through name resolution to host-bridge dispatch (`Undefined
29/// builtin`). Retaining the complete loaded graph here for the cache entry's
30/// lifetime keeps every transitively reachable registry and module state
31/// upgradeable at call time, for every fire — not just the first.
32pub(crate) struct ResolvedLazyCallable {
33    pub(crate) exports: BTreeMap<String, Arc<VmClosure>>,
34    /// Intentionally unread: retained solely to keep the loaded module graph's
35    /// function registries and module states alive for this cache entry (the
36    /// same liveness role [`crate::value::RetainedModuleScope`] plays for a
37    /// single retained closure, generalized across the whole import graph).
38    #[allow(dead_code)]
39    pub(crate) retained_module_graph: ModuleCache,
40}
41
42pub(crate) type LazyCallableResolution = Arc<ResolvedLazyCallable>;
43pub(crate) type LazyCallableModuleCache =
44    Arc<VmMutex<BTreeMap<PathBuf, Arc<tokio::sync::OnceCell<LazyCallableResolution>>>>>;
45
46/// RAII guard that starts a tracing span on creation and ends it on drop.
47pub(crate) struct ScopeSpan(u64);
48
49impl ScopeSpan {
50    pub(crate) fn new(kind: crate::tracing::SpanKind, name: String) -> Self {
51        Self(crate::tracing::span_start(kind, name))
52    }
53}
54
55/// Cancellation-safe host deadline state. The guard owns the shared state so
56/// dropping an in-flight `execute_with_timeout` future restores the previous
57/// deadline and poisons the abandoned VM even though the future still holds
58/// `&mut Vm`.
59pub(crate) struct ExecutionDeadlineState {
60    origin: Instant,
61    /// Nanoseconds from `origin`, plus one so zero remains the inactive value.
62    deadline_offset: AtomicU64,
63    /// Set when the host drops a polled execution future. Arbitrary async
64    /// cancellation cannot unwind interpreter state, so subsequent execution
65    /// entries fail loudly instead of resuming a partial frame.
66    abandoned: AtomicBool,
67}
68
69impl ExecutionDeadlineState {
70    pub(crate) fn new(origin: Instant, deadline: Option<Instant>) -> Arc<Self> {
71        Arc::new(Self {
72            origin,
73            deadline_offset: AtomicU64::new(Self::encode(origin, deadline)),
74            abandoned: AtomicBool::new(false),
75        })
76    }
77
78    #[inline]
79    pub(crate) fn is_active(&self) -> bool {
80        self.deadline_offset.load(Ordering::Acquire) != 0
81    }
82
83    #[inline]
84    pub(crate) fn is_abandoned(&self) -> bool {
85        self.abandoned.load(Ordering::Acquire)
86    }
87
88    pub(crate) fn fork(&self) -> Arc<Self> {
89        let state = Self::new(self.origin, self.current());
90        state
91            .abandoned
92            .store(self.is_abandoned(), Ordering::Release);
93        state
94    }
95
96    pub(crate) fn current(&self) -> Option<Instant> {
97        let encoded = self.deadline_offset.load(Ordering::Acquire);
98        (encoded != 0)
99            .then(|| self.origin + std::time::Duration::from_nanos(encoded.saturating_sub(1)))
100    }
101
102    pub(crate) fn install(self: &Arc<Self>, deadline: Instant) -> ExecutionDeadlineGuard {
103        let previous = self.deadline_offset.load(Ordering::Acquire);
104        let requested = Self::encode(self.origin, Some(deadline));
105        let active = if previous == 0 {
106            requested
107        } else {
108            previous.min(requested)
109        };
110        self.deadline_offset.store(active, Ordering::Release);
111        ExecutionDeadlineGuard {
112            state: Arc::clone(self),
113            previous,
114            completed: false,
115        }
116    }
117
118    fn encode(origin: Instant, deadline: Option<Instant>) -> u64 {
119        deadline.map_or(0, |deadline| {
120            let nanos = deadline.saturating_duration_since(origin).as_nanos();
121            u64::try_from(nanos)
122                .unwrap_or(u64::MAX - 1)
123                .saturating_add(1)
124        })
125    }
126}
127
128pub(crate) struct ExecutionDeadlineGuard {
129    state: Arc<ExecutionDeadlineState>,
130    previous: u64,
131    completed: bool,
132}
133
134impl ExecutionDeadlineGuard {
135    /// Mark an awaited execution as terminal before restoring its prior host
136    /// deadline. Dropping without this acknowledgement poisons the VM.
137    pub(crate) fn complete(mut self) {
138        self.completed = true;
139    }
140}
141
142impl Drop for ExecutionDeadlineGuard {
143    fn drop(&mut self) {
144        self.state
145            .deadline_offset
146            .store(self.previous, Ordering::Release);
147        if !self.completed {
148            self.state.abandoned.store(true, Ordering::Release);
149            crate::orchestration::clear_pipeline_on_finish();
150        }
151    }
152}
153
154impl Drop for ScopeSpan {
155    fn drop(&mut self) {
156        crate::tracing::span_end(self.0);
157    }
158}
159
160#[derive(Clone)]
161pub(crate) struct LocalSlot {
162    pub(crate) value: VmValue,
163    pub(crate) initialized: bool,
164    pub(crate) synced: bool,
165}
166
167impl Drop for LocalSlot {
168    fn drop(&mut self) {
169        // Slot locals hold script values directly (e.g. a `var` bound to a
170        // deeply nested list). When a frame is torn down, the default
171        // recursive drop of such a value would overflow the native stack and
172        // abort the process. For the overwhelmingly common scalar slot this is
173        // a single `matches!` check and then the normal trivial drop; only a
174        // nested container is moved out and torn down iteratively, so hot
175        // frame teardown is unaffected.
176        if crate::value::recursion::is_recursive_container(&self.value) {
177            crate::value::recursion::dismantle(std::mem::replace(&mut self.value, VmValue::Nil));
178        }
179    }
180}
181
182#[derive(Clone)]
183pub(crate) struct InterruptHandler {
184    pub(crate) handle: i64,
185    pub(crate) signals: Vec<String>,
186    pub(crate) once: bool,
187    pub(crate) graceful_timeout_ms: Option<u64>,
188    pub(crate) handler: VmValue,
189}
190
191/// Call frame for function execution.
192pub(crate) struct CallFrame {
193    pub(crate) chunk: ChunkRef,
194    /// VM-local inline-cache set for this frame's chunk. Computed once at
195    /// frame entry so hot opcode dispatch can index cache feedback directly
196    /// instead of hashing the chunk id on every cached opcode.
197    pub(crate) inline_cache_set: usize,
198    pub(crate) ip: usize,
199    pub(crate) stack_base: usize,
200    pub(crate) saved_env: VmEnv,
201    /// Env snapshot captured at call-time, *after* argument binding. Used
202    /// by the debugger's `restartFrame` to rewind this frame to its
203    /// entry state (re-binding args from the original values) without
204    /// re-entering the call site. Cheap to clone because `VmEnv` is
205    /// already cloned into `saved_env` on every call. `None` for
206    /// scratch frames (evaluate, import init) where restart isn't
207    /// meaningful.
208    pub(crate) initial_env: Option<VmEnv>,
209    pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
210    /// Iterator stack depth to restore when this frame unwinds.
211    pub(crate) saved_iterator_depth: usize,
212    /// Function name for stack traces (empty for top-level pipeline).
213    pub(crate) fn_name: String,
214    /// Number of arguments actually passed by the caller (for default arg support).
215    pub(crate) argc: usize,
216    /// Saved VM_SOURCE_DIR to restore when this frame is popped.
217    /// Set when entering a closure that originated from an imported module.
218    pub(crate) saved_source_dir: Option<std::path::PathBuf>,
219    /// Module-local named functions available to symbolic calls within this frame.
220    pub(crate) module_functions: Option<ModuleFunctionRegistry>,
221    /// Shared module-level env for top-level `var` / `let` bindings of
222    /// this frame's originating module. Looked up after `self.env` and
223    /// before `self.globals` by `GetVar` / `SetVar`, giving each module
224    /// its own live static state that persists across calls. See the
225    /// `module_state` field on `VmClosure` for the full rationale.
226    pub(crate) module_state: Option<crate::value::ModuleState>,
227    /// Slot-indexed locals for compiler-resolved names in this frame.
228    pub(crate) local_slots: Vec<LocalSlot>,
229    /// Env scope index that corresponds to compiler local scope depth 0.
230    pub(crate) local_scope_base: usize,
231    /// Current compiler local scope depth, updated by PushScope/PopScope.
232    pub(crate) local_scope_depth: usize,
233}
234
235pub(crate) struct InlineCacheSite {
236    pub(crate) cache_set: usize,
237    pub(crate) slot_count: usize,
238    pub(crate) slot: Option<usize>,
239}
240
241impl CallFrame {
242    #[inline]
243    pub(crate) fn inline_cache_site_for_previous_op(&self) -> InlineCacheSite {
244        let op_offset = self.ip.saturating_sub(1);
245        InlineCacheSite {
246            cache_set: self.inline_cache_set,
247            slot_count: self.chunk.inline_cache_slot_count(),
248            slot: self.chunk.inline_cache_slot(op_offset),
249        }
250    }
251}
252
253/// Exception handler for try/catch.
254pub(crate) struct ExceptionHandler {
255    pub(crate) catch_ip: usize,
256    pub(crate) stack_depth: usize,
257    pub(crate) frame_depth: usize,
258    pub(crate) env_scope_depth: usize,
259    /// When present, this catch only handles errors whose enum_name matches.
260    pub(crate) error_type: Option<crate::value::HarnStr>,
261}
262
263/// A structured-concurrency nursery (`scope { }`). Tasks spawned while this
264/// scope is innermost record their id here; `TaskScopeExit` joins them.
265pub(crate) struct TaskScope {
266    /// Ids of tasks spawned in this scope that have not been explicitly
267    /// `await`ed away. Joined (normal exit) or cancelled (unwind) on close.
268    pub(crate) task_ids: Vec<String>,
269    /// Frame depth at which the scope was opened, for unwind pruning.
270    pub(crate) frame_depth: usize,
271    /// Env scope depth at open, for unwind pruning.
272    pub(crate) env_scope_depth: usize,
273}
274
275/// Iterator state for for-in loops.
276pub(crate) enum IterState {
277    Vec {
278        items: Arc<Vec<VmValue>>,
279        idx: usize,
280    },
281    Dict {
282        entries: Arc<crate::value::DictMap>,
283        keys: Vec<String>,
284        idx: usize,
285    },
286    Channel {
287        receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
288        close: std::sync::Arc<crate::value::VmChannelCloseState>,
289    },
290    Generator {
291        gen: Arc<crate::value::VmGenerator>,
292    },
293    Stream {
294        stream: Arc<crate::value::VmStream>,
295    },
296    /// Step through a lazy range without materializing a Vec.
297    /// Inclusive ranges keep `end` as an actual value so `i64::MAX to i64::MAX`
298    /// still yields one item instead of overflowing a one-past-end sentinel.
299    Range {
300        next: i64,
301        end: i64,
302        inclusive: bool,
303        done: bool,
304    },
305    VmIter {
306        handle: crate::vm::iter::VmIterHandle,
307    },
308}
309
310#[derive(Clone)]
311pub(crate) enum VmBuiltinDispatch {
312    Sync(VmBuiltinFn),
313    Async(VmAsyncBuiltinFn),
314}
315
316#[derive(Clone)]
317pub(crate) struct VmBuiltinEntry {
318    pub(crate) name: Arc<str>,
319    pub(crate) dispatch: VmBuiltinDispatch,
320}
321
322/// The Harn bytecode virtual machine.
323pub struct Vm {
324    pub(crate) stack: Vec<VmValue>,
325    pub(crate) env: VmEnv,
326    pub(crate) output: String,
327    pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
328    pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
329    pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
330    /// Numeric side index for builtins. Name-keyed maps remain authoritative;
331    /// this index is the hot path for direct builtin bytecode and callback refs.
332    pub(crate) builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
333    /// IDs with detected name collisions. Collided names safely fall back to
334    /// the authoritative name-keyed lookup path.
335    pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
336    /// Iterator state for for-in loops.
337    pub(crate) iterators: Vec<IterState>,
338    /// Call frame stack.
339    pub(crate) frames: Vec<CallFrame>,
340    /// Exception handler stack.
341    pub(crate) exception_handlers: Vec<ExceptionHandler>,
342    /// Spawned async task handles.
343    pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
344    /// Shared process-local synchronization primitives inherited by child VMs.
345    pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
346    /// Shared process-local cells, maps, and mailboxes inherited by child VMs.
347    pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
348    /// Per-isolate inline cache entries. `inline_cache_set_by_chunk` maps a
349    /// compiled chunk identity to an index in this vector at frame entry; the
350    /// dispatch loop uses the frame-local index for per-op reads/writes.
351    pub(crate) inline_cache_sets: Vec<Vec<crate::chunk::InlineCacheEntry>>,
352    pub(crate) inline_cache_set_by_chunk: HashMap<u64, usize>,
353    /// VM-scoped pool registry inherited by child VMs and scoped into Tokio tasks.
354    pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
355    /// Reader leases opened by package_snapshot_open in this execution tree.
356    /// Child VMs share the registry; the final VM drop releases abandoned
357    /// leases without touching concurrent executions.
358    pub(crate) package_snapshot_registry: Arc<crate::stdlib::PackageSnapshotRegistry>,
359    /// Shared task/channel wait graph for this VM execution tree.
360    pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
361    /// Permits acquired by lexical synchronization blocks in this VM.
362    pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
363    /// Locks held by an ancestor VM that is *suspended on this VM's execution*:
364    /// an inline async-builtin child runs while its parent is parked
365    /// mid-instruction still holding these permits. Re-acquiring more permits
366    /// than the primitive can grant is a provably-unresolvable self-deadlock, so
367    /// HARN-ORC-011 fires across the child boundary. Empty for new concurrent
368    /// tasks (`spawn`/`parallel`/triggers), where the parent keeps running and
369    /// blocking can be legitimately resolvable.
370    pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
371    /// Structured-concurrency nursery stack. Each `scope { }` block pushes a
372    /// `TaskScope`; tasks spawned while it is innermost register their id here.
373    /// On normal exit (`TaskScopeExit`) the scope's tasks are joined and the
374    /// first error propagates; on unwind they are cancelled. Modeled on
375    /// `held_sync_guards` (push on enter, prune/cancel on frame/handler exit).
376    pub(crate) task_scopes: Vec<TaskScope>,
377    /// Counter for generating unique task IDs.
378    pub(crate) task_counter: u64,
379    /// Counter for logical runtime-context task groups.
380    pub(crate) runtime_context_counter: u64,
381    /// Logical runtime task context visible through `runtime_context()`.
382    pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
383    /// Active deadline stack: (deadline_instant, frame_depth).
384    pub(crate) deadlines: Vec<(Instant, usize)>,
385    /// Uncatchable wall-clock deadline imposed by the embedding host.
386    pub(crate) execution_deadline: Arc<ExecutionDeadlineState>,
387    /// Breakpoints, keyed by source-file path so a breakpoint at line N
388    /// in `auto.harn` doesn't also fire when execution hits line N in an
389    /// imported lib. The empty-string key is a wildcard used by callers
390    /// that don't track source paths (legacy `set_breakpoints` API).
391    pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
392    /// Function-name breakpoints. Any closure call whose
393    /// `CompiledFunction.name` matches an entry here raises a stop on
394    /// entry, regardless of the call site's file or line. Lets the IDE
395    /// break on `llm_call` / `host_run_pipeline` / any user pipeline
396    /// function without pinning down a source location first.
397    pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
398    /// Latched on `push_closure_frame` when the callee's name matches
399    /// `function_breakpoints`; consumed by the next step so the stop is
400    /// reported with reason="function breakpoint" and the breakpoint
401    /// name available for the DAP `stopped` event.
402    pub(crate) pending_function_bp: Option<String>,
403    /// Whether the VM is in step mode.
404    pub(crate) step_mode: bool,
405    /// The frame depth at which stepping started (for step-over).
406    pub(crate) step_frame_depth: usize,
407    /// Whether the VM is currently stopped at a debug point.
408    pub(crate) stopped: bool,
409    /// Last source line executed (to detect line changes).
410    pub(crate) last_line: usize,
411    /// Source directory for resolving imports.
412    pub(crate) source_dir: Option<std::path::PathBuf>,
413    /// Installed-package identity retained for the active lazy pipeline.
414    pub(crate) package_execution_guard:
415        Option<Arc<harn_modules::package_execution::PackageExecutionGuard>>,
416    /// Modules currently being imported (cycle prevention).
417    pub(crate) imported_paths: Vec<std::path::PathBuf>,
418    /// Imports that hit an in-progress module (an import cycle) and so could
419    /// not bind inline. Drained by `flush_deferred_cyclic_imports` once the
420    /// involved modules finish loading.
421    pub(crate) deferred_cyclic_imports: Vec<super::modules::DeferredCyclicImport>,
422    /// Loaded module cache keyed by canonical or synthetic module path.
423    pub(crate) module_cache: ModuleCache,
424    /// Immutable hydrated module bytecode shared across fresh VM isolates.
425    /// Runtime closures, registries, state, and init execution are not cached.
426    pub(crate) prepared_module_cache: crate::PreparedModuleCache,
427    /// Optional timing recorder shared by this VM execution tree.
428    pub(crate) module_phase_recorder: Option<super::ModulePhaseRecorder>,
429    /// Lazy manifest modules initialized by any child in this execution tree.
430    /// The complete export set is retained so handlers and predicates from the
431    /// same module share one module state across repeated child invocations.
432    pub(crate) lazy_callable_modules: LazyCallableModuleCache,
433    /// Source text keyed by canonical or synthetic module path for debugger retrieval.
434    pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, String>>,
435    /// Source file path for error reporting.
436    pub(crate) source_file: Option<String>,
437    /// Source text for error reporting.
438    pub(crate) source_text: Option<String>,
439    /// Line-coverage accumulator. `Some` only while a coverage session is
440    /// active (see [`crate::coverage`]); folded into the global report on drop.
441    pub(crate) coverage: Option<crate::coverage::Coverage>,
442    /// Optional bridge for delegating unknown builtins in bridge mode.
443    pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
444    /// Builtins denied by sandbox mode (`--deny` / `--allow` flags).
445    pub(crate) denied_builtins: Arc<HashSet<String>>,
446    /// Cancellation token for cooperative graceful shutdown (set by parent).
447    pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
448    pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
449    /// Remaining instruction-boundary checks before a requested host
450    /// cancellation is forcefully raised. This gives `is_cancelled()` loops a
451    /// deterministic chance to return cleanly without letting non-cooperative
452    /// CPU-bound code run forever.
453    pub(crate) cancel_grace_instructions_remaining: Option<usize>,
454    /// User-visible interrupt handlers registered through `std/signal`.
455    pub(crate) interrupt_handlers: Vec<InterruptHandler>,
456    pub(crate) next_interrupt_handle: i64,
457    pub(crate) pending_interrupt_signal: Option<String>,
458    pub(crate) interrupted: bool,
459    pub(crate) dispatching_interrupt: bool,
460    pub(crate) interrupt_handler_deadline: Option<Instant>,
461    /// Captured stack trace from the most recent error (fn_name, line, col).
462    pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
463    /// Yield channel sender for generator execution. When set, `Op::Yield`
464    /// sends values through this channel instead of being a no-op.
465    pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
466    /// Project root directory (detected via harn.toml).
467    /// Used as base directory for metadata, store, and checkpoint operations.
468    pub(crate) project_root: Option<std::path::PathBuf>,
469    /// Global constants (e.g. `pi`, `e`). Checked as a fallback in `GetVar`
470    /// after the environment, so user-defined variables can shadow them.
471    pub(crate) globals: Arc<crate::value::DictMap>,
472    /// Optional debugger hook invoked when execution advances to a new source line.
473    pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
474    /// Effective runtime ceilings for this VM execution.
475    pub(crate) runtime_limits: RuntimeLimits,
476}
477
478/// Reusable VM baseline for hosts that need many clean executions with the
479/// same stable builtin/source setup.
480///
481/// The baseline intentionally does not snapshot execution state. Each
482/// instantiation gets fresh stacks, frames, tasks, cancellation fields, sync
483/// primitives, shared cells/maps/mailboxes, and debug state. Builtin tables are
484/// shared through `Arc` until a per-execution rebind needs copy-on-write.
485#[derive(Clone)]
486pub struct VmBaseline {
487    builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
488    async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
489    builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
490    builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
491    builtin_id_collisions: Arc<HashSet<BuiltinId>>,
492    source_dir: Option<std::path::PathBuf>,
493    source_file: Option<String>,
494    source_text: Option<String>,
495    project_root: Option<std::path::PathBuf>,
496    globals: Arc<crate::value::DictMap>,
497    denied_builtins: Arc<HashSet<String>>,
498    prepared_module_cache: crate::PreparedModuleCache,
499    runtime_limits: RuntimeLimits,
500}
501
502impl VmBaseline {
503    pub fn from_vm(vm: &Vm) -> Self {
504        Self {
505            builtins: Arc::clone(&vm.builtins),
506            async_builtins: Arc::clone(&vm.async_builtins),
507            builtin_metadata: Arc::clone(&vm.builtin_metadata),
508            builtins_by_id: Arc::clone(&vm.builtins_by_id),
509            builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
510            source_dir: vm.source_dir.clone(),
511            source_file: vm.source_file.clone(),
512            source_text: vm.source_text.clone(),
513            project_root: vm.project_root.clone(),
514            globals: Arc::clone(&vm.globals),
515            denied_builtins: Arc::clone(&vm.denied_builtins),
516            prepared_module_cache: vm.prepared_module_cache.clone(),
517            runtime_limits: vm.runtime_limits,
518        }
519    }
520
521    pub fn instantiate(&self) -> Vm {
522        let mut source_cache = BTreeMap::new();
523        if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
524            source_cache.insert(std::path::PathBuf::from(file), text.clone());
525        }
526        if let Some(dir) = &self.source_dir {
527            crate::stdlib::set_thread_source_dir(dir);
528        }
529
530        let mut vm = Vm {
531            stack: Vec::with_capacity(256),
532            env: VmEnv::new(),
533            output: String::new(),
534            builtins: Arc::clone(&self.builtins),
535            async_builtins: Arc::clone(&self.async_builtins),
536            builtin_metadata: Arc::clone(&self.builtin_metadata),
537            builtins_by_id: Arc::clone(&self.builtins_by_id),
538            builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
539            iterators: Vec::new(),
540            frames: Vec::new(),
541            exception_handlers: Vec::new(),
542            spawned_tasks: BTreeMap::new(),
543            sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
544            shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
545            inline_cache_sets: Vec::new(),
546            inline_cache_set_by_chunk: HashMap::new(),
547            pool_registry: crate::stdlib::pool::new_pool_registry(),
548            package_snapshot_registry: Arc::new(Default::default()),
549            wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
550            held_sync_guards: Vec::new(),
551            inherited_held_keys: Arc::new(Vec::new()),
552            task_scopes: Vec::new(),
553            task_counter: 0,
554            runtime_context_counter: 0,
555            runtime_context: crate::runtime_context::RuntimeContext::root(),
556            deadlines: Vec::new(),
557            execution_deadline: super::execution::new_execution_deadline_state(None),
558            breakpoints: BTreeMap::new(),
559            function_breakpoints: std::collections::BTreeSet::new(),
560            pending_function_bp: None,
561            step_mode: false,
562            step_frame_depth: 0,
563            stopped: false,
564            last_line: 0,
565            source_dir: self.source_dir.clone(),
566            package_execution_guard: None,
567            imported_paths: Vec::new(),
568            deferred_cyclic_imports: Vec::new(),
569            module_cache: Arc::new(BTreeMap::new()),
570            prepared_module_cache: self.prepared_module_cache.clone(),
571            module_phase_recorder: None,
572            lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
573            source_cache: Arc::new(source_cache),
574            source_file: self.source_file.clone(),
575            source_text: self.source_text.clone(),
576            coverage: crate::coverage::for_primary(self.source_file.as_deref()),
577            bridge: None,
578            denied_builtins: Arc::clone(&self.denied_builtins),
579            cancel_token: None,
580            interrupt_signal_token: None,
581            cancel_grace_instructions_remaining: None,
582            interrupt_handlers: Vec::new(),
583            next_interrupt_handle: 1,
584            pending_interrupt_signal: None,
585            interrupted: false,
586            dispatching_interrupt: false,
587            interrupt_handler_deadline: None,
588            error_stack_trace: Vec::new(),
589            yield_sender: None,
590            project_root: self.project_root.clone(),
591            globals: Arc::clone(&self.globals),
592            debug_hook: None,
593            runtime_limits: self.runtime_limits,
594        };
595
596        crate::stdlib::rebind_execution_state_builtins(&mut vm);
597        vm
598    }
599}
600
601impl Vm {
602    pub(crate) fn ensure_execution_available(&self) -> Result<(), VmError> {
603        if self.execution_deadline.is_abandoned() {
604            return Err(VmError::AbandonedExecution);
605        }
606        Ok(())
607    }
608
609    pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
610        chunk
611            .local_slots
612            .iter()
613            .map(|_| LocalSlot {
614                value: VmValue::Nil,
615                initialized: false,
616                synced: false,
617            })
618            .collect()
619    }
620
621    pub(crate) fn bind_param_slots(
622        slots: &mut [LocalSlot],
623        func: &crate::chunk::CompiledFunction,
624        args: &[VmValue],
625        synced: bool,
626    ) {
627        Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
628    }
629
630    pub(crate) fn bind_param_slots_args(
631        slots: &mut [LocalSlot],
632        func: &crate::chunk::CompiledFunction,
633        args: &super::CallArgs<'_>,
634        synced: bool,
635    ) {
636        let param_count = func.params.len();
637        for (i, _param) in func.params.iter().enumerate() {
638            if i >= slots.len() {
639                break;
640            }
641            if func.has_rest_param && i == param_count - 1 {
642                let rest_args = args.to_vec_from(i);
643                slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
644                slots[i].initialized = true;
645                slots[i].synced = synced;
646            } else if let Some(arg) = args.get(i) {
647                slots[i].value = arg.clone();
648                slots[i].initialized = true;
649                slots[i].synced = synced;
650            }
651        }
652    }
653
654    pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
655        let mut vars = self.env.all_variables();
656        let Some(frame) = self.frames.last() else {
657            return vars;
658        };
659        for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
660            if slot.initialized && info.scope_depth <= frame.local_scope_depth {
661                vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
662            }
663        }
664        vars
665    }
666
667    pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
668        let frames = &mut self.frames;
669        let env = &mut self.env;
670        let Some(frame) = frames.last_mut() else {
671            return;
672        };
673        let local_scope_base = frame.local_scope_base;
674        let local_scope_depth = frame.local_scope_depth;
675        for (slot, info) in frame
676            .local_slots
677            .iter_mut()
678            .zip(frame.chunk.local_slots.iter())
679        {
680            if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
681                slot.synced = true;
682                let scope_idx = local_scope_base + info.scope_depth;
683                while env.scopes.len() <= scope_idx {
684                    env.push_scope();
685                }
686                // Slot-backed locals are never boxed captures (those resolve
687                // to `None` in the compiler and are defined straight into the
688                // env as `Cell`s), so syncing a slot always yields a `Value`.
689                Arc::make_mut(&mut env.scopes[scope_idx].vars).insert(
690                    info.name.clone(),
691                    crate::value::Binding::Value {
692                        value: slot.value.clone(),
693                        mutable: info.mutable,
694                    },
695                );
696            }
697        }
698    }
699
700    pub(crate) fn closure_call_env_for_current_frame(
701        &self,
702        closure: &crate::value::VmClosure,
703    ) -> VmEnv {
704        if closure.module_state().is_some() {
705            return closure.env.cloned_for_call();
706        }
707        let call_env = Self::closure_call_env(&self.env, closure);
708        // Same compile-time short-circuit as the env walk in
709        // `closure_call_env`: when the callee body never resolves an
710        // outer name through the env, injecting closure-typed *slot*
711        // locals from the caller's frame is wasted work too.
712        if !closure.func.chunk.references_outer_names {
713            return call_env;
714        }
715        let mut call_env = call_env;
716        let Some(frame) = self.frames.last() else {
717            return call_env;
718        };
719        for (slot, info) in frame
720            .local_slots
721            .iter()
722            .zip(frame.chunk.local_slots.iter())
723            .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
724        {
725            if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
726                let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
727            }
728        }
729        call_env
730    }
731
732    pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
733        let frame = self.frames.last()?;
734        let idx = self.active_local_slot_index(name)?;
735        frame.local_slots.get(idx).map(|slot| slot.value.clone())
736    }
737
738    /// Returns the slot index of an initialized active local with the given
739    /// name, walking from innermost to outermost scope. Used by legacy by-name
740    /// hot paths that still want to mutate the slot value in place without
741    /// paying a defensive `VmValue::clone` first.
742    pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
743        let frame = self.frames.last()?;
744        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
745            if info.name == name && info.scope_depth <= frame.local_scope_depth {
746                if let Some(slot) = frame.local_slots.get(idx) {
747                    if slot.initialized {
748                        return Some(idx);
749                    }
750                }
751            }
752        }
753        None
754    }
755
756    pub(crate) fn assign_active_local_slot(
757        &mut self,
758        name: &str,
759        value: VmValue,
760        debug: bool,
761    ) -> Result<bool, VmError> {
762        let Some(frame) = self.frames.last_mut() else {
763            return Ok(false);
764        };
765        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
766            if info.name == name && info.scope_depth <= frame.local_scope_depth {
767                if !debug && !info.mutable {
768                    return Err(VmError::ImmutableAssignment(name.to_string()));
769                }
770                if let Some(slot) = frame.local_slots.get_mut(idx) {
771                    crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
772                    slot.initialized = true;
773                    slot.synced = false;
774                    return Ok(true);
775                }
776            }
777        }
778        Ok(false)
779    }
780
781    pub fn new() -> Self {
782        Self {
783            stack: Vec::with_capacity(256),
784            env: VmEnv::new(),
785            output: String::new(),
786            builtins: Arc::new(BTreeMap::new()),
787            async_builtins: Arc::new(BTreeMap::new()),
788            builtin_metadata: Arc::new(BTreeMap::new()),
789            builtins_by_id: Arc::new(HashMap::new()),
790            builtin_id_collisions: Arc::new(HashSet::new()),
791            iterators: Vec::new(),
792            frames: Vec::new(),
793            exception_handlers: Vec::new(),
794            spawned_tasks: BTreeMap::new(),
795            sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
796            shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
797            inline_cache_sets: Vec::new(),
798            inline_cache_set_by_chunk: HashMap::new(),
799            pool_registry: crate::stdlib::pool::new_pool_registry(),
800            package_snapshot_registry: Arc::new(Default::default()),
801            wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
802            held_sync_guards: Vec::new(),
803            inherited_held_keys: Arc::new(Vec::new()),
804            task_scopes: Vec::new(),
805            task_counter: 0,
806            runtime_context_counter: 0,
807            runtime_context: crate::runtime_context::RuntimeContext::root(),
808            deadlines: Vec::new(),
809            execution_deadline: super::execution::new_execution_deadline_state(None),
810            breakpoints: BTreeMap::new(),
811            function_breakpoints: std::collections::BTreeSet::new(),
812            pending_function_bp: None,
813            step_mode: false,
814            step_frame_depth: 0,
815            stopped: false,
816            last_line: 0,
817            source_dir: None,
818            package_execution_guard: None,
819            imported_paths: Vec::new(),
820            deferred_cyclic_imports: Vec::new(),
821            module_cache: Arc::new(BTreeMap::new()),
822            prepared_module_cache: crate::PreparedModuleCache::default(),
823            module_phase_recorder: None,
824            lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
825            source_cache: Arc::new(BTreeMap::new()),
826            source_file: None,
827            source_text: None,
828            coverage: crate::coverage::for_primary(None),
829            bridge: None,
830            denied_builtins: Arc::new(HashSet::new()),
831            cancel_token: None,
832            interrupt_signal_token: None,
833            cancel_grace_instructions_remaining: None,
834            interrupt_handlers: Vec::new(),
835            next_interrupt_handle: 1,
836            pending_interrupt_signal: None,
837            interrupted: false,
838            dispatching_interrupt: false,
839            interrupt_handler_deadline: None,
840            error_stack_trace: Vec::new(),
841            yield_sender: None,
842            project_root: None,
843            globals: Arc::new(crate::value::DictMap::new()),
844            debug_hook: None,
845            runtime_limits: RuntimeLimits::default(),
846        }
847    }
848
849    pub fn baseline(&self) -> VmBaseline {
850        VmBaseline::from_vm(self)
851    }
852
853    /// Replace the scoped immutable module-template cache used by this VM.
854    /// Fresh runtime module state is still instantiated for every VM.
855    pub fn set_prepared_module_cache(&mut self, cache: crate::PreparedModuleCache) {
856        self.prepared_module_cache = cache;
857    }
858
859    /// Return the effective runtime limit profile for this VM.
860    pub fn runtime_limits(&self) -> RuntimeLimits {
861        self.runtime_limits
862    }
863
864    /// Return a host/debug report describing the VM's effective runtime limits.
865    pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
866        self.runtime_limits.report()
867    }
868
869    /// Returns true if any debugging affordance is active — DAP hook,
870    /// line breakpoints, or function breakpoints. Call-site code uses
871    /// this to decide whether to capture per-frame restart snapshots
872    /// (`initial_env`, `initial_local_slots`); without a debugger those
873    /// snapshots are dead weight, so skipping them removes two
874    /// allocations from every function call hot path.
875    ///
876    /// All three signals are stable across a function call's lifetime
877    /// (they're set before pipeline execution starts), so the gate is
878    /// consistent between frame creation and any later `restart_frame`
879    /// invocation. The three `is_empty` checks compile to a handful of
880    /// branch-predicted memory probes — cheaper than a single
881    /// `BTreeMap` clone, which is what we're avoiding.
882    #[inline]
883    pub(crate) fn debugger_attached(&self) -> bool {
884        self.debug_hook.is_some()
885            || !self.breakpoints.is_empty()
886            || !self.function_breakpoints.is_empty()
887    }
888
889    /// Set the bridge for delegating unknown builtins in bridge mode.
890    pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
891        self.bridge = Some(bridge);
892    }
893
894    /// Set builtins that are denied in sandbox mode.
895    /// When called, the given builtin names will produce a permission error.
896    pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
897        self.denied_builtins = Arc::new(denied);
898    }
899
900    /// Set source info for error reporting (file path and source text).
901    pub fn set_source_info(&mut self, file: &str, text: &str) {
902        self.source_file = Some(file.to_string());
903        self.source_text = Some(text.to_string());
904        if let Some(cov) = self.coverage.as_mut() {
905            cov.set_primary_file(file);
906        }
907        Arc::make_mut(&mut self.source_cache)
908            .insert(std::path::PathBuf::from(file), text.to_string());
909    }
910
911    /// Initialize execution (push the initial frame).
912    pub fn start(&mut self, chunk: &Chunk) -> Result<(), VmError> {
913        self.ensure_execution_available()?;
914        // The top-level pipeline frame captures env at start so
915        // restartFrame on the outermost frame rewinds to the
916        // pre-pipeline state — basically "restart session" in
917        // debugger terms. Skipped when no debugger is attached:
918        // the snapshot is dead weight in that case and dominates
919        // call-overhead bench numbers (~5-10%).
920        let debugger = self.debugger_attached();
921        let initial_env = if debugger {
922            Some(self.env.clone())
923        } else {
924            None
925        };
926        let initial_local_slots = if debugger {
927            Some(Self::fresh_local_slots(chunk))
928        } else {
929            None
930        };
931        let chunk = Arc::new(chunk.clone());
932        let local_slots = Self::fresh_local_slots(&chunk);
933        let inline_cache_set = self.inline_cache_set_index_for_chunk(&chunk);
934        self.frames.push(CallFrame {
935            chunk,
936            inline_cache_set,
937            ip: 0,
938            stack_base: self.stack.len(),
939            saved_env: self.env.clone(),
940            initial_env,
941            initial_local_slots,
942            saved_iterator_depth: self.iterators.len(),
943            fn_name: String::new(),
944            argc: 0,
945            saved_source_dir: None,
946            module_functions: None,
947            module_state: None,
948            local_slots,
949            local_scope_base: self.env.scope_depth().saturating_sub(1),
950            local_scope_depth: 0,
951        });
952        Ok(())
953    }
954
955    /// Create a child VM that shares builtins and env but has fresh execution state.
956    /// Used for parallel/spawn to fork the VM for concurrent tasks.
957    pub(crate) fn child_vm(&self) -> Vm {
958        Vm {
959            stack: Vec::with_capacity(64),
960            env: self.env.clone(),
961            output: String::new(),
962            builtins: Arc::clone(&self.builtins),
963            async_builtins: Arc::clone(&self.async_builtins),
964            builtin_metadata: Arc::clone(&self.builtin_metadata),
965            builtins_by_id: Arc::clone(&self.builtins_by_id),
966            builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
967            iterators: Vec::new(),
968            frames: Vec::new(),
969            exception_handlers: Vec::new(),
970            spawned_tasks: BTreeMap::new(),
971            sync_runtime: self.sync_runtime.clone(),
972            shared_state_runtime: self.shared_state_runtime.clone(),
973            inline_cache_sets: Vec::new(),
974            inline_cache_set_by_chunk: HashMap::new(),
975            pool_registry: self.pool_registry.clone(),
976            package_snapshot_registry: self.package_snapshot_registry.clone(),
977            wait_for_graph: self.wait_for_graph.clone(),
978            held_sync_guards: Vec::new(),
979            inherited_held_keys: Arc::new(Vec::new()),
980            task_scopes: Vec::new(),
981            task_counter: 0,
982            runtime_context_counter: self.runtime_context_counter,
983            runtime_context: self.runtime_context.clone(),
984            deadlines: self.deadlines.clone(),
985            execution_deadline: self.execution_deadline.fork(),
986            breakpoints: BTreeMap::new(),
987            function_breakpoints: std::collections::BTreeSet::new(),
988            pending_function_bp: None,
989            step_mode: false,
990            step_frame_depth: 0,
991            stopped: false,
992            last_line: 0,
993            source_dir: self.source_dir.clone(),
994            package_execution_guard: self.package_execution_guard.clone(),
995            imported_paths: Vec::new(),
996            deferred_cyclic_imports: Vec::new(),
997            module_cache: Arc::clone(&self.module_cache),
998            prepared_module_cache: self.prepared_module_cache.clone(),
999            module_phase_recorder: self.module_phase_recorder.clone(),
1000            lazy_callable_modules: Arc::clone(&self.lazy_callable_modules),
1001            source_cache: Arc::clone(&self.source_cache),
1002            source_file: self.source_file.clone(),
1003            source_text: self.source_text.clone(),
1004            coverage: crate::coverage::for_primary(self.source_file.as_deref()),
1005            bridge: self.bridge.clone(),
1006            denied_builtins: Arc::clone(&self.denied_builtins),
1007            cancel_token: self.cancel_token.clone(),
1008            interrupt_signal_token: self.interrupt_signal_token.clone(),
1009            cancel_grace_instructions_remaining: None,
1010            interrupt_handlers: Vec::new(),
1011            next_interrupt_handle: 1,
1012            pending_interrupt_signal: None,
1013            interrupted: self.interrupted,
1014            dispatching_interrupt: false,
1015            interrupt_handler_deadline: None,
1016            error_stack_trace: Vec::new(),
1017            yield_sender: None,
1018            project_root: self.project_root.clone(),
1019            globals: Arc::clone(&self.globals),
1020            debug_hook: None,
1021            runtime_limits: self.runtime_limits,
1022        }
1023    }
1024
1025    /// Create a child VM for external adapters that need to invoke Harn
1026    /// closures while sharing the parent's builtins, globals, and module state.
1027    pub(crate) fn child_vm_for_host(&self) -> Vm {
1028        self.child_vm()
1029    }
1030
1031    /// Request cancellation for every outstanding child task owned by this VM
1032    /// and then abort the join handles. This prevents un-awaited spawned tasks
1033    /// from outliving their parent execution scope.
1034    pub(crate) fn cancel_spawned_tasks(&mut self) {
1035        for (_, task) in std::mem::take(&mut self.spawned_tasks) {
1036            task.cancel_token
1037                .store(true, std::sync::atomic::Ordering::SeqCst);
1038            task.handle.abort();
1039        }
1040    }
1041
1042    /// Set the source directory for import resolution and introspection.
1043    /// Also auto-detects the project root if not already set.
1044    pub fn set_source_dir(&mut self, dir: &std::path::Path) {
1045        let dir = crate::stdlib::process::normalize_context_path(dir);
1046        self.source_dir = Some(dir.clone());
1047        crate::stdlib::set_thread_source_dir(&dir);
1048        // Auto-detect project root if not explicitly set.
1049        if self.project_root.is_none() {
1050            self.project_root = crate::stdlib::process::find_project_root(&dir);
1051        }
1052    }
1053
1054    /// Explicitly set the project root directory.
1055    /// Used by ACP/CLI to override auto-detection.
1056    pub fn set_project_root(&mut self, root: &std::path::Path) {
1057        self.project_root = Some(root.to_path_buf());
1058    }
1059
1060    /// Get only the explicit or auto-detected project root, without falling
1061    /// back to `source_dir`.
1062    pub(crate) fn explicit_project_root(&self) -> Option<&std::path::Path> {
1063        self.project_root.as_deref()
1064    }
1065
1066    /// Get the project root directory, falling back to source_dir.
1067    pub fn project_root(&self) -> Option<&std::path::Path> {
1068        self.project_root.as_deref().or(self.source_dir.as_deref())
1069    }
1070
1071    /// Return all registered builtin names (sync + async).
1072    pub fn builtin_names(&self) -> Vec<String> {
1073        let mut names: Vec<String> = self.builtins.keys().cloned().collect();
1074        names.extend(self.async_builtins.keys().cloned());
1075        names
1076    }
1077
1078    /// Return discoverable metadata for registered builtins.
1079    pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
1080        self.builtin_metadata.values().cloned().collect()
1081    }
1082
1083    /// Return discoverable metadata for a registered builtin name.
1084    pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
1085        self.builtin_metadata.get(name)
1086    }
1087
1088    /// Set a global constant (e.g. `pi`, `e`).
1089    /// Stored separately from the environment so user-defined variables can shadow them.
1090    pub fn set_global(&mut self, name: &str, value: VmValue) {
1091        Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
1092    }
1093
1094    /// Read a previously-installed global (the value `set_global` /
1095    /// `set_harness` recorded). Returns `None` for unknown names.
1096    /// Hosts use this to look up runtime-installed capability handles
1097    /// (e.g. the `harness` slot) without having to track them
1098    /// separately.
1099    pub fn global(&self, name: &str) -> Option<&VmValue> {
1100        self.globals.get(name)
1101    }
1102
1103    /// Install the script's `Harness` capability handle as the `harness`
1104    /// global so the auto-call emitted by `Compiler::compile()` (for
1105    /// `fn main(harness: Harness)` entrypoints) can read it. Hosts that
1106    /// drive the VM directly (CLI, MCP server, composition runtime) call
1107    /// this once before `execute()`.
1108    pub fn set_harness(&mut self, harness: crate::harness::Harness) {
1109        self.set_global("harness", harness.into_vm_value());
1110    }
1111
1112    /// Get the captured output.
1113    pub fn output(&self) -> &str {
1114        &self.output
1115    }
1116
1117    /// Drain and return the captured output, leaving the buffer empty.
1118    /// Used by the async-builtin dispatch path to forward closure output
1119    /// from a child VM back to its parent.
1120    pub fn take_output(&mut self) -> String {
1121        std::mem::take(&mut self.output)
1122    }
1123
1124    /// Append text to this VM's captured output. Used to forward output
1125    /// from child VMs (e.g. closures invoked via `call_closure_pub`)
1126    /// back into the parent stream.
1127    pub fn append_output(&mut self, text: &str) {
1128        self.output.push_str(text);
1129    }
1130
1131    pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
1132        self.stack.pop().ok_or(VmError::StackUnderflow)
1133    }
1134
1135    pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
1136        self.stack.last().ok_or(VmError::StackUnderflow)
1137    }
1138
1139    pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
1140        match c {
1141            Constant::String(s) => Ok(s.as_str()),
1142            _ => Err(VmError::TypeError("expected string constant".into())),
1143        }
1144    }
1145
1146    pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
1147        let depth = self.env.scope_depth();
1148        self.held_sync_guards
1149            .retain(|guard| guard.env_scope_depth < depth);
1150        // A `scope { }` torn down without a normal `TaskScopeExit` (break /
1151        // continue out of it) leaves a dangling nursery — cancel its tasks.
1152        self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
1153    }
1154
1155    pub(crate) fn release_sync_guards_after_unwind(
1156        &mut self,
1157        frame_depth: usize,
1158        env_scope_depth: usize,
1159    ) {
1160        self.held_sync_guards.retain(|guard| {
1161            guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
1162        });
1163        // Cancel nurseries opened above the catch handler (a `throw` unwound
1164        // past their `TaskScopeExit`).
1165        self.cancel_task_scopes_where(|s| {
1166            !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
1167        });
1168    }
1169
1170    pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
1171        self.held_sync_guards
1172            .retain(|guard| guard.frame_depth != frame_depth);
1173        // Cancel any nursery whose `scope {}` block belonged to the frame being
1174        // torn down (e.g. a `return` jumped past its `TaskScopeExit`).
1175        self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
1176    }
1177
1178    pub(crate) fn adopt_sync_permit_for_current_scope(
1179        &mut self,
1180        permit: crate::value::VmSyncPermitHandle,
1181    ) {
1182        if permit.is_released()
1183            || self
1184                .held_sync_guards
1185                .iter()
1186                .any(|guard| guard._permit.same_lease(&permit))
1187        {
1188            return;
1189        }
1190        self.held_sync_guards
1191            .push(crate::synchronization::VmSyncHeldGuard {
1192                _permit: permit,
1193                frame_depth: self.frames.len(),
1194                env_scope_depth: self.env.scope_depth(),
1195            });
1196    }
1197
1198    /// Deregister a task id from every open nursery (it was explicitly
1199    /// `await`ed, so it must not be double-joined or cancelled at scope exit).
1200    pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
1201        for scope in &mut self.task_scopes {
1202            scope.task_ids.retain(|t| t != id);
1203        }
1204    }
1205
1206    /// Cancel and remove every task scope matching `doomed`, aborting its bound
1207    /// tasks (used when a `scope {}` is torn down without a normal join).
1208    fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
1209        let mut i = 0;
1210        while i < self.task_scopes.len() {
1211            if doomed(&self.task_scopes[i]) {
1212                let scope = self.task_scopes.remove(i);
1213                for id in &scope.task_ids {
1214                    if let Some(task) = self.spawned_tasks.remove(id) {
1215                        task.cancel_token
1216                            .store(true, std::sync::atomic::Ordering::SeqCst);
1217                        task.handle.abort();
1218                    }
1219                }
1220            } else {
1221                i += 1;
1222            }
1223        }
1224    }
1225
1226    /// Total live permits this VM already holds for `kind:key`. The held-set is
1227    /// tiny (bounded by lexical nesting and explicit sync acquisitions), so this
1228    /// scan is cheap and only runs on the rare blocking-acquire path.
1229    pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
1230        let own: u32 = self
1231            .held_sync_guards
1232            .iter()
1233            .filter(|guard| {
1234                !guard._permit.is_released()
1235                    && guard._permit.kind() == kind
1236                    && guard._permit.key() == key
1237            })
1238            .map(|guard| guard._permit.permits())
1239            .sum();
1240        let inherited: u32 = self
1241            .inherited_held_keys
1242            .iter()
1243            .filter(|held| held.kind == kind && held.key == key)
1244            .map(|held| held.permits)
1245            .sum();
1246        own + inherited
1247    }
1248
1249    /// Every live sync permit held by this VM *and* its suspended ancestors: the
1250    /// transitive held-set seen by an inline child.
1251    pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1252        let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1253            .held_sync_guards
1254            .iter()
1255            .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1256            .collect();
1257        keys.extend(self.inherited_held_keys.iter().cloned());
1258        keys
1259    }
1260
1261    /// Clone a child VM for an **inline, same-task** execution (an async builtin
1262    /// awaited while this VM is parked, or a user closure that builtin runs and
1263    /// awaits). The child inherits this VM's transitive held-lock keys so a
1264    /// re-acquire of a parent-held lock is caught as a self-deadlock
1265    /// (HARN-ORC-011). Use plain `child_vm()` for new concurrent tasks.
1266    pub(crate) fn child_vm_inline(&self) -> Vm {
1267        let mut child = self.child_vm();
1268        child.inherited_held_keys = Arc::new(self.combined_held_keys());
1269        child
1270    }
1271}
1272
1273impl Drop for Vm {
1274    fn drop(&mut self) {
1275        if let Some(coverage) = self.coverage.take() {
1276            crate::coverage::merge_into_global(coverage);
1277        }
1278        self.cancel_spawned_tasks();
1279    }
1280}
1281
1282impl Default for Vm {
1283    fn default() -> Self {
1284        Self::new()
1285    }
1286}
1287
1288#[cfg(test)]
1289mod tests {
1290
1291    use super::*;
1292
1293    fn baseline_with_stdlib(source: &str) -> VmBaseline {
1294        let mut vm = Vm::new();
1295        crate::register_vm_stdlib(&mut vm);
1296        vm.set_source_info("baseline_test.harn", source);
1297        vm.set_global(
1298            "stable_global",
1299            VmValue::String(arcstr::ArcStr::from("baseline")),
1300        );
1301        vm.baseline()
1302    }
1303
1304    #[test]
1305    fn vm_baseline_instantiates_clean_mutable_execution_state() {
1306        let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1307
1308        let mut dirty = baseline.instantiate();
1309        dirty.stack.push(VmValue::Int(42));
1310        dirty.output.push_str("dirty");
1311        dirty.task_counter = 9;
1312        dirty.runtime_context_counter = 7;
1313        dirty
1314            .error_stack_trace
1315            .push(("main".to_string(), 1, 1, None));
1316
1317        let clean = baseline.instantiate();
1318        assert!(clean.stack.is_empty());
1319        assert!(clean.output.is_empty());
1320        assert!(clean.frames.is_empty());
1321        assert!(clean.exception_handlers.is_empty());
1322        assert!(clean.spawned_tasks.is_empty());
1323        assert!(clean.held_sync_guards.is_empty());
1324        assert_eq!(clean.task_counter, 0);
1325        assert_eq!(clean.runtime_context_counter, 0);
1326        assert!(clean.deadlines.is_empty());
1327        assert!(clean.cancel_token.is_none());
1328        assert!(clean.interrupt_handlers.is_empty());
1329        assert!(clean.error_stack_trace.is_empty());
1330        assert!(clean.bridge.is_none());
1331        assert!(clean
1332            .globals
1333            .get("stable_global")
1334            .is_some_and(|value| value.display() == "baseline"));
1335    }
1336
1337    #[tokio::test]
1338    async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1339        let mut parent = Vm::new();
1340        let permit = parent
1341            .sync_runtime
1342            .acquire("mutex", "v:test", 1, 1, None, None)
1343            .await
1344            .unwrap()
1345            .unwrap();
1346        parent
1347            .held_sync_guards
1348            .push(crate::synchronization::VmSyncHeldGuard {
1349                _permit: permit,
1350                frame_depth: 0,
1351                env_scope_depth: 0,
1352            });
1353        assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1354
1355        // An inline child (async builtin awaited while the parent is parked, or
1356        // a closure the builtin runs inline) inherits the held key, so a
1357        // re-acquire is caught as a cross-context self-deadlock (HARN-ORC-011)
1358        // — even transitively through a further inline child.
1359        let inline = parent.child_vm_inline();
1360        assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1361        assert_eq!(
1362            inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1363            1
1364        );
1365
1366        // A new concurrent task (spawn / parallel / trigger) does NOT inherit:
1367        // blocking on a parent-held lock there is legitimately resolvable, so
1368        // flagging it would be a false positive.
1369        let concurrent = parent.child_vm();
1370        assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1371    }
1372
1373    #[test]
1374    fn vm_reports_effective_runtime_limits() {
1375        let vm = Vm::new();
1376
1377        assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1378        assert_eq!(
1379            vm.runtime_limit_report().entries.len(),
1380            crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1381        );
1382        assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1383        assert_eq!(
1384            vm.baseline().instantiate().runtime_limits(),
1385            vm.runtime_limits()
1386        );
1387    }
1388
1389    #[tokio::test(flavor = "current_thread")]
1390    async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1391        let local = tokio::task::LocalSet::new();
1392        local
1393            .run_until(async {
1394                let source = r#"
1395pipeline main() {
1396  const cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1397  __io_println(shared_get(cell))
1398  shared_set(cell, shared_get(cell) + 1)
1399}"#;
1400                let chunk = crate::compile_source(source).expect("compile");
1401                let baseline = baseline_with_stdlib(source);
1402
1403                let mut first = baseline.instantiate();
1404                first.execute(&chunk).await.expect("first execute");
1405                assert_eq!(first.output(), "0\n");
1406
1407                let mut second = baseline.instantiate();
1408                second.execute(&chunk).await.expect("second execute");
1409                assert_eq!(
1410                    second.output(),
1411                    "0\n",
1412                    "shared state created by the first VM must not leak into the next baseline instance"
1413                );
1414            })
1415            .await;
1416    }
1417}