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