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        }
154    }
155}
156
157impl Drop for ScopeSpan {
158    fn drop(&mut self) {
159        crate::tracing::span_end(self.0);
160    }
161}
162
163#[derive(Clone)]
164pub(crate) struct LocalSlot {
165    pub(crate) value: VmValue,
166    pub(crate) initialized: bool,
167    pub(crate) synced: bool,
168}
169
170impl Drop for LocalSlot {
171    fn drop(&mut self) {
172        // Slot locals hold script values directly (e.g. a `let` bound to a
173        // deeply nested list). When a frame is torn down, the default
174        // recursive drop of such a value would overflow the native stack and
175        // abort the process. For the overwhelmingly common scalar slot this is
176        // a single `matches!` check and then the normal trivial drop; only a
177        // nested container is moved out and torn down iteratively, so hot
178        // frame teardown is unaffected.
179        if crate::value::recursion::is_recursive_container(&self.value) {
180            crate::value::recursion::dismantle(std::mem::replace(&mut self.value, VmValue::Nil));
181        }
182    }
183}
184
185#[derive(Clone)]
186pub(crate) struct InterruptHandler {
187    pub(crate) handle: i64,
188    pub(crate) signals: Vec<String>,
189    pub(crate) once: bool,
190    pub(crate) graceful_timeout_ms: Option<u64>,
191    pub(crate) handler: VmValue,
192}
193
194/// Call frame for function execution.
195pub(crate) struct CallFrame {
196    pub(crate) chunk: ChunkRef,
197    /// VM-local inline-cache set for this frame's chunk. Computed once at
198    /// frame entry so hot opcode dispatch can index cache feedback directly
199    /// instead of hashing the chunk id on every cached opcode.
200    pub(crate) inline_cache_set: usize,
201    pub(crate) ip: usize,
202    pub(crate) stack_base: usize,
203    pub(crate) saved_env: VmEnv,
204    /// Env snapshot captured at call-time, *after* argument binding. Used
205    /// by the debugger's `restartFrame` to rewind this frame to its
206    /// entry state (re-binding args from the original values) without
207    /// re-entering the call site. Cheap to clone because `VmEnv` is
208    /// already cloned into `saved_env` on every call. `None` for
209    /// scratch frames (evaluate, import init) where restart isn't
210    /// meaningful.
211    pub(crate) initial_env: Option<VmEnv>,
212    pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
213    /// Iterator stack depth to restore when this frame unwinds.
214    pub(crate) saved_iterator_depth: usize,
215    /// Function name for stack traces (empty for top-level pipeline); a
216    /// refcount-bump share of [`CompiledFunction::name`].
217    pub(crate) fn_name: crate::value::HarnStr,
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<crate::value::HarnStr>,
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    /// Static effect specs for source-visible capability/privileged builtins.
363    /// Keeping this beside the numeric dispatch entry makes pure direct calls
364    /// pay one `Option` branch instead of consulting the manifest by name.
365    pub(crate) recorded_effects: Option<&'static [harn_builtin_meta::EffectSpec]>,
366}
367
368/// The Harn bytecode virtual machine.
369pub struct Vm {
370    pub(crate) stack: Vec<VmValue>,
371    pub(crate) env: VmEnv,
372    pub(crate) output: String,
373    pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
374    pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
375    /// Host-supplied implementations keyed by the typed capability surface
376    /// they implement. Unlike ordinary builtins these names never enter the
377    /// source namespace: they are reachable only through a `Harness*` value.
378    pub(crate) capability_methods:
379        Arc<BTreeMap<harn_builtin_meta::CapabilityId, BTreeMap<String, VmBuiltinDispatch>>>,
380    pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
381    /// Numeric side index for builtins. Name-keyed maps remain authoritative;
382    /// this index is the hot path for direct builtin bytecode and callback refs.
383    pub(crate) builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
384    /// IDs with detected name collisions. Collided names safely fall back to
385    /// the authoritative name-keyed lookup path.
386    pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
387    /// Iterator state for for-in loops.
388    pub(crate) iterators: Vec<IterState>,
389    /// Call frame stack.
390    pub(crate) frames: Vec<CallFrame>,
391    /// Exception handler stack.
392    pub(crate) exception_handlers: Vec<ExceptionHandler>,
393    /// Spawned async task handles.
394    pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
395    /// Shared terminal process-exit latch for this execution tree.
396    pub(crate) process_exit_request: Arc<ProcessExitRequest>,
397    /// Shared process-local synchronization primitives inherited by child VMs.
398    pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
399    /// Shared process-local cells, maps, and mailboxes inherited by child VMs.
400    pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
401    /// Per-isolate inline cache entries. `inline_cache_set_by_chunk` maps a
402    /// compiled chunk identity to an index in this vector at frame entry; the
403    /// dispatch loop uses the frame-local index for per-op reads/writes.
404    pub(crate) inline_cache_sets: Vec<Vec<crate::chunk::InlineCacheEntry>>,
405    pub(crate) inline_cache_set_by_chunk: HashMap<u64, usize>,
406    /// VM-scoped pool registry inherited by child VMs and scoped into Tokio tasks.
407    pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
408    /// Inline LLM fixtures and observations shared by this VM execution tree.
409    pub(crate) llm_mock_context: crate::llm::mock::LlmMockContext,
410    /// Reader leases opened by package_snapshot_open in this execution tree.
411    /// Child VMs share the registry; the final VM drop releases abandoned
412    /// leases without touching concurrent executions.
413    pub(crate) package_snapshot_registry: Arc<crate::stdlib::PackageSnapshotRegistry>,
414    /// Shared task/channel wait graph for this VM execution tree.
415    pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
416    /// Permits acquired by lexical synchronization blocks in this VM.
417    pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
418    /// Locks held by an ancestor VM that is *suspended on this VM's execution*:
419    /// an inline async-builtin child runs while its parent is parked
420    /// mid-instruction still holding these permits. Re-acquiring more permits
421    /// than the primitive can grant is a provably-unresolvable self-deadlock, so
422    /// HARN-ORC-011 fires across the child boundary. Empty for new concurrent
423    /// tasks (`spawn`/`parallel`/triggers), where the parent keeps running and
424    /// blocking can be legitimately resolvable.
425    pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
426    /// Structured-concurrency nursery stack. Each `scope { }` block pushes a
427    /// `TaskScope`; tasks spawned while it is innermost register their id here.
428    /// On normal exit (`TaskScopeExit`) the scope's tasks are joined and the
429    /// first error propagates; on unwind they are cancelled. Modeled on
430    /// `held_sync_guards` (push on enter, prune/cancel on frame/handler exit).
431    pub(crate) task_scopes: Vec<TaskScope>,
432    /// Counter for generating unique task IDs.
433    pub(crate) task_counter: u64,
434    /// Counter for logical runtime-context task groups.
435    pub(crate) runtime_context_counter: u64,
436    /// Logical runtime task context visible through `runtime_context()`.
437    pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
438    /// Active deadline stack: (deadline_instant, frame_depth).
439    pub(crate) deadlines: Vec<(Instant, usize)>,
440    /// Uncatchable wall-clock deadline imposed by the embedding host.
441    pub(crate) execution_deadline: Arc<ExecutionDeadlineState>,
442    /// Breakpoints, keyed by source-file path so a breakpoint at line N
443    /// in `auto.harn` doesn't also fire when execution hits line N in an
444    /// imported lib. The empty-string key is a wildcard used by callers
445    /// that don't track source paths (legacy `set_breakpoints` API).
446    pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
447    /// Function-name breakpoints. Any closure call whose
448    /// `CompiledFunction.name` matches an entry here raises a stop on
449    /// entry, regardless of the call site's file or line. Lets the IDE
450    /// break on `llm_call` / `host_run_pipeline` / any user pipeline
451    /// function without pinning down a source location first.
452    pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
453    /// Latched on `push_closure_frame` when the callee's name matches
454    /// `function_breakpoints`; consumed by the next step so the stop is
455    /// reported with reason="function breakpoint" and the breakpoint
456    /// name available for the DAP `stopped` event.
457    pub(crate) pending_function_bp: Option<String>,
458    /// Whether the VM is in step mode.
459    pub(crate) step_mode: bool,
460    /// The frame depth at which stepping started (for step-over).
461    pub(crate) step_frame_depth: usize,
462    /// Whether the VM is currently stopped at a debug point.
463    pub(crate) stopped: bool,
464    /// Last source line executed (to detect line changes).
465    pub(crate) last_line: usize,
466    /// Source directory for resolving imports.
467    pub(crate) source_dir: Option<std::path::PathBuf>,
468    /// Installed-package identity retained for the active lazy pipeline.
469    pub(crate) package_execution_guard:
470        Option<Arc<harn_modules::package_execution::PackageExecutionGuard>>,
471    /// Modules currently being imported (cycle prevention).
472    pub(crate) imported_paths: Vec<std::path::PathBuf>,
473    /// Imports that hit an in-progress module (an import cycle) and so could
474    /// not bind inline. Drained by `flush_deferred_cyclic_imports` once the
475    /// involved modules finish loading.
476    pub(crate) deferred_cyclic_imports: Vec<super::modules::DeferredCyclicImport>,
477    /// Loaded module cache keyed by canonical or synthetic module path.
478    pub(crate) module_cache: ModuleCache,
479    /// Immutable hydrated module bytecode shared across fresh VM isolates.
480    /// Runtime closures, registries, state, and init execution are not cached.
481    pub(crate) prepared_module_cache: crate::PreparedModuleCache,
482    /// Authority provenance used for the next root module graph. This can only
483    /// move from ordinary user code to trusted host dispatch before loading.
484    pub(crate) module_provenance: crate::module_artifact::ModuleProvenance,
485    /// Optional timing recorder shared by this VM execution tree.
486    pub(crate) module_phase_recorder: Option<super::ModulePhaseRecorder>,
487    /// Lazy manifest modules initialized by any child in this execution tree.
488    /// The complete export set is retained so handlers and predicates from the
489    /// same module share one module state across repeated child invocations.
490    pub(crate) lazy_callable_modules: LazyCallableModuleCache,
491    /// Source text keyed by canonical or synthetic module path for debugger
492    /// retrieval. Entries share the bytes owned by
493    /// [`crate::module_source`] rather than copying them.
494    pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, Arc<str>>>,
495    /// This spawn's import-graph link table, when the entry-chunk cache proved
496    /// one current. Lets module loading name a module's artifact from a recorded
497    /// digest instead of reading the file to recompute the same digest. `None`
498    /// means every module takes the ordinary read-and-hash path.
499    pub(crate) graph_link_table: Option<Arc<crate::context_manifest::GraphLinkTable>>,
500    /// Verified, closed-program module templates for this execution tree.
501    /// Unlike the generic graph link table, absence is never interpreted as a
502    /// source-cache miss by the linked-program path.
503    pub(crate) linked_program_repository:
504        Option<Arc<crate::linked_program::LinkedProgramRepository>>,
505    /// Source file path for error reporting.
506    pub(crate) source_file: Option<String>,
507    /// Source text for error reporting.
508    pub(crate) source_text: Option<String>,
509    /// Line-coverage accumulator. `Some` only while a coverage session is
510    /// active (see [`crate::coverage`]); folded into the global report on drop.
511    pub(crate) coverage: Option<crate::coverage::Coverage>,
512    /// Optional bridge for delegating unknown builtins in bridge mode.
513    pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
514    /// Builtins denied by sandbox mode (`--deny` / `--allow` flags).
515    pub(crate) denied_builtins: Arc<HashSet<String>>,
516    /// Cancellation token for cooperative graceful shutdown (set by parent).
517    pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
518    pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
519    /// Remaining instruction-boundary checks before a requested host
520    /// cancellation is forcefully raised. This gives `is_cancelled()` loops a
521    /// deterministic chance to return cleanly without letting non-cooperative
522    /// CPU-bound code run forever.
523    pub(crate) cancel_grace_instructions_remaining: Option<usize>,
524    /// User-visible interrupt handlers registered through `std/signal`.
525    pub(crate) interrupt_handlers: Vec<InterruptHandler>,
526    pub(crate) next_interrupt_handle: i64,
527    pub(crate) pending_interrupt_signal: Option<String>,
528    pub(crate) interrupted: bool,
529    pub(crate) dispatching_interrupt: bool,
530    pub(crate) interrupt_handler_deadline: Option<Instant>,
531    /// Captured stack trace from the most recent error (fn_name, line, col).
532    pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
533    /// Yield channel sender for generator execution. When set, `Op::Yield`
534    /// sends values through this channel instead of being a no-op.
535    pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
536    /// Project root directory (detected via harn.toml).
537    /// Used as base directory for metadata, store, and checkpoint operations.
538    pub(crate) project_root: Option<std::path::PathBuf>,
539    /// Global constants (e.g. `pi`, `e`). Checked as a fallback in `GetVar`
540    /// after the environment, so user-defined variables can shadow them.
541    pub(crate) globals: Arc<crate::value::DictMap>,
542    /// Root authority injected by the embedder for the compiler-generated
543    /// `main(harness: Harness)` call. This is deliberately not an environment
544    /// binding or global: user code can receive it only through an explicit
545    /// parameter.
546    pub(crate) root_harness: Option<VmValue>,
547    /// Runtime effect receipt state (shared recorder + per-VM call memo).
548    pub(crate) runtime_effects: crate::orchestration::RuntimeEffectState,
549    /// Optional debugger hook invoked when execution advances to a new source line.
550    pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
551    /// Effective runtime ceilings for this VM execution.
552    pub(crate) runtime_limits: RuntimeLimits,
553}
554
555/// Reusable VM baseline for hosts that need many clean executions with the
556/// same stable builtin/source setup.
557///
558/// The baseline intentionally does not snapshot execution state. Each
559/// instantiation gets fresh stacks, frames, tasks, cancellation fields, sync
560/// primitives, shared cells/maps/mailboxes, and debug state. Builtin tables are
561/// shared through `Arc` until a per-execution rebind needs copy-on-write.
562#[derive(Clone)]
563pub struct VmBaseline {
564    builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
565    async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
566    capability_methods:
567        Arc<BTreeMap<harn_builtin_meta::CapabilityId, BTreeMap<String, VmBuiltinDispatch>>>,
568    builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
569    builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
570    builtin_id_collisions: Arc<HashSet<BuiltinId>>,
571    source_dir: Option<std::path::PathBuf>,
572    source_file: Option<String>,
573    source_text: Option<String>,
574    project_root: Option<std::path::PathBuf>,
575    globals: Arc<crate::value::DictMap>,
576    root_harness: Option<VmValue>,
577    denied_builtins: Arc<HashSet<String>>,
578    prepared_module_cache: crate::PreparedModuleCache,
579    module_provenance: crate::module_artifact::ModuleProvenance,
580    /// Carried so a VM rebuilt from this baseline keeps resolving modules
581    /// through the same link table the original spawn was handed.
582    graph_link_table: Option<Arc<crate::context_manifest::GraphLinkTable>>,
583    linked_program_repository: Option<Arc<crate::linked_program::LinkedProgramRepository>>,
584    runtime_limits: RuntimeLimits,
585}
586
587impl VmBaseline {
588    pub fn from_vm(vm: &Vm) -> Self {
589        Self {
590            builtins: Arc::clone(&vm.builtins),
591            async_builtins: Arc::clone(&vm.async_builtins),
592            capability_methods: Arc::clone(&vm.capability_methods),
593            builtin_metadata: Arc::clone(&vm.builtin_metadata),
594            builtins_by_id: Arc::clone(&vm.builtins_by_id),
595            builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
596            source_dir: vm.source_dir.clone(),
597            source_file: vm.source_file.clone(),
598            source_text: vm.source_text.clone(),
599            project_root: vm.project_root.clone(),
600            globals: Arc::clone(&vm.globals),
601            root_harness: vm.root_harness.clone(),
602            denied_builtins: Arc::clone(&vm.denied_builtins),
603            prepared_module_cache: vm.prepared_module_cache.clone(),
604            module_provenance: vm.module_provenance,
605            graph_link_table: vm.graph_link_table.clone(),
606            linked_program_repository: vm.linked_program_repository.clone(),
607            runtime_limits: vm.runtime_limits,
608        }
609    }
610
611    pub fn instantiate(&self) -> Vm {
612        crate::initialize_runtime_assets();
613        let mut source_cache = BTreeMap::new();
614        if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
615            source_cache.insert(std::path::PathBuf::from(file), Arc::from(text.as_str()));
616        }
617        if let Some(dir) = &self.source_dir {
618            crate::stdlib::set_thread_source_dir(dir);
619        }
620
621        let mut vm = Vm {
622            stack: Vec::with_capacity(256),
623            env: VmEnv::new(),
624            output: String::new(),
625            builtins: Arc::clone(&self.builtins),
626            async_builtins: Arc::clone(&self.async_builtins),
627            capability_methods: Arc::clone(&self.capability_methods),
628            builtin_metadata: Arc::clone(&self.builtin_metadata),
629            builtins_by_id: Arc::clone(&self.builtins_by_id),
630            builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
631            iterators: Vec::new(),
632            frames: Vec::new(),
633            exception_handlers: Vec::new(),
634            spawned_tasks: BTreeMap::new(),
635            process_exit_request: Arc::new(ProcessExitRequest::new()),
636            sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
637            shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
638            inline_cache_sets: Vec::new(),
639            inline_cache_set_by_chunk: HashMap::new(),
640            pool_registry: crate::stdlib::pool::new_pool_registry(),
641            llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
642            package_snapshot_registry: Arc::new(Default::default()),
643            wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
644            held_sync_guards: Vec::new(),
645            inherited_held_keys: Arc::new(Vec::new()),
646            task_scopes: Vec::new(),
647            task_counter: 0,
648            runtime_context_counter: 0,
649            runtime_context: crate::runtime_context::RuntimeContext::root(),
650            deadlines: Vec::new(),
651            execution_deadline: super::execution::new_execution_deadline_state(None),
652            breakpoints: BTreeMap::new(),
653            function_breakpoints: std::collections::BTreeSet::new(),
654            pending_function_bp: None,
655            step_mode: false,
656            step_frame_depth: 0,
657            stopped: false,
658            last_line: 0,
659            source_dir: self.source_dir.clone(),
660            package_execution_guard: None,
661            imported_paths: Vec::new(),
662            deferred_cyclic_imports: Vec::new(),
663            module_cache: Arc::new(BTreeMap::new()),
664            prepared_module_cache: self.prepared_module_cache.clone(),
665            module_provenance: self.module_provenance,
666            module_phase_recorder: None,
667            lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
668            source_cache: Arc::new(source_cache),
669            graph_link_table: self.graph_link_table.clone(),
670            linked_program_repository: self.linked_program_repository.clone(),
671            source_file: self.source_file.clone(),
672            source_text: self.source_text.clone(),
673            coverage: crate::coverage::for_primary(self.source_file.as_deref()),
674            bridge: None,
675            denied_builtins: Arc::clone(&self.denied_builtins),
676            cancel_token: None,
677            interrupt_signal_token: None,
678            cancel_grace_instructions_remaining: None,
679            interrupt_handlers: Vec::new(),
680            next_interrupt_handle: 1,
681            pending_interrupt_signal: None,
682            interrupted: false,
683            dispatching_interrupt: false,
684            interrupt_handler_deadline: None,
685            error_stack_trace: Vec::new(),
686            yield_sender: None,
687            project_root: self.project_root.clone(),
688            globals: Arc::clone(&self.globals),
689            root_harness: self.root_harness.clone(),
690            runtime_effects: crate::orchestration::RuntimeEffectState::fresh(),
691            debug_hook: None,
692            runtime_limits: self.runtime_limits,
693        };
694
695        crate::stdlib::rebind_execution_state_builtins(&mut vm);
696        vm
697    }
698}
699
700impl Vm {
701    pub(crate) fn ensure_execution_available(&self) -> Result<(), VmError> {
702        if self.execution_deadline.is_abandoned() {
703            return Err(VmError::AbandonedExecution);
704        }
705        Ok(())
706    }
707
708    pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
709        chunk
710            .local_slots
711            .iter()
712            .map(|_| LocalSlot {
713                value: VmValue::Nil,
714                initialized: false,
715                synced: false,
716            })
717            .collect()
718    }
719
720    pub(crate) fn bind_param_slots(
721        slots: &mut [LocalSlot],
722        func: &crate::chunk::CompiledFunction,
723        args: &[VmValue],
724        synced: bool,
725    ) {
726        Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
727    }
728
729    pub(crate) fn bind_param_slots_args(
730        slots: &mut [LocalSlot],
731        func: &crate::chunk::CompiledFunction,
732        args: &super::CallArgs<'_>,
733        synced: bool,
734    ) {
735        let param_count = func.params.len();
736        for (i, _param) in func.params.iter().enumerate() {
737            if i >= slots.len() {
738                break;
739            }
740            if func.has_rest_param && i == param_count - 1 {
741                let rest_args = args.to_vec_from(i);
742                slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
743                slots[i].initialized = true;
744                slots[i].synced = synced;
745            } else if let Some(arg) = args.get(i) {
746                slots[i].value = arg.clone();
747                slots[i].initialized = true;
748                slots[i].synced = synced;
749            }
750        }
751    }
752
753    pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
754        let mut vars = self.env.all_variables();
755        let Some(frame) = self.frames.last() else {
756            return vars;
757        };
758        for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
759            if slot.initialized && info.scope_depth <= frame.local_scope_depth {
760                vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
761            }
762        }
763        vars
764    }
765
766    pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
767        let frames = &mut self.frames;
768        let env = &mut self.env;
769        let Some(frame) = frames.last_mut() else {
770            return;
771        };
772        let local_scope_base = frame.local_scope_base;
773        let local_scope_depth = frame.local_scope_depth;
774        for (slot, info) in frame
775            .local_slots
776            .iter_mut()
777            .zip(frame.chunk.local_slots.iter())
778        {
779            if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
780                slot.synced = true;
781                let scope_idx = local_scope_base + info.scope_depth;
782                while env.scopes.len() <= scope_idx {
783                    env.push_scope();
784                }
785                // Slot-backed locals are never boxed captures (those resolve
786                // to `None` in the compiler and are defined straight into the
787                // env as `Cell`s), so syncing a slot always yields a `Value`.
788                Arc::make_mut(&mut env.scopes[scope_idx].vars).insert(
789                    info.name.clone(),
790                    crate::value::Binding::Value {
791                        value: slot.value.clone(),
792                        mutable: info.mutable,
793                    },
794                );
795            }
796        }
797    }
798
799    pub(crate) fn closure_call_env_for_current_frame(
800        &self,
801        closure: &crate::value::VmClosure,
802    ) -> VmEnv {
803        if closure.module_state().is_some() {
804            return closure.env.cloned_for_call();
805        }
806        let call_env = Self::closure_call_env(&self.env, closure);
807        // Same compile-time short-circuit as the env walk in
808        // `closure_call_env`: when the callee body never resolves an
809        // outer name through the env, injecting closure-typed *slot*
810        // locals from the caller's frame is wasted work too.
811        if !closure.func.chunk.references_outer_names {
812            return call_env;
813        }
814        let mut call_env = call_env;
815        let Some(frame) = self.frames.last() else {
816            return call_env;
817        };
818        for (slot, info) in frame
819            .local_slots
820            .iter()
821            .zip(frame.chunk.local_slots.iter())
822            .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
823        {
824            if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
825                let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
826            }
827        }
828        call_env
829    }
830
831    pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
832        let frame = self.frames.last()?;
833        let idx = self.active_local_slot_index(name)?;
834        frame.local_slots.get(idx).map(|slot| slot.value.clone())
835    }
836
837    /// Returns the slot index of an initialized active local with the given
838    /// name, walking from innermost to outermost scope. Used by legacy by-name
839    /// hot paths that still want to mutate the slot value in place without
840    /// paying a defensive `VmValue::clone` first.
841    pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
842        let frame = self.frames.last()?;
843        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
844            if info.name == name && info.scope_depth <= frame.local_scope_depth {
845                if let Some(slot) = frame.local_slots.get(idx) {
846                    if slot.initialized {
847                        return Some(idx);
848                    }
849                }
850            }
851        }
852        None
853    }
854
855    pub(crate) fn assign_active_local_slot(
856        &mut self,
857        name: &str,
858        value: VmValue,
859        debug: bool,
860    ) -> Result<bool, VmError> {
861        let Some(frame) = self.frames.last_mut() else {
862            return Ok(false);
863        };
864        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
865            if info.name == name && info.scope_depth <= frame.local_scope_depth {
866                if !debug && !info.mutable {
867                    return Err(VmError::ImmutableAssignment(name.to_string()));
868                }
869                if let Some(slot) = frame.local_slots.get_mut(idx) {
870                    crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
871                    slot.initialized = true;
872                    slot.synced = false;
873                    return Ok(true);
874                }
875            }
876        }
877        Ok(false)
878    }
879
880    pub fn new() -> Self {
881        crate::initialize_runtime_assets();
882        Self {
883            stack: Vec::with_capacity(256),
884            env: VmEnv::new(),
885            output: String::new(),
886            builtins: Arc::new(BTreeMap::new()),
887            async_builtins: Arc::new(BTreeMap::new()),
888            capability_methods: Arc::new(BTreeMap::new()),
889            builtin_metadata: Arc::new(BTreeMap::new()),
890            builtins_by_id: Arc::new(HashMap::new()),
891            builtin_id_collisions: Arc::new(HashSet::new()),
892            iterators: Vec::new(),
893            frames: Vec::new(),
894            exception_handlers: Vec::new(),
895            spawned_tasks: BTreeMap::new(),
896            process_exit_request: Arc::new(ProcessExitRequest::new()),
897            sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
898            shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
899            inline_cache_sets: Vec::new(),
900            inline_cache_set_by_chunk: HashMap::new(),
901            pool_registry: crate::stdlib::pool::new_pool_registry(),
902            llm_mock_context: crate::llm::mock::LlmMockContext::for_new_vm(),
903            package_snapshot_registry: Arc::new(Default::default()),
904            wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
905            held_sync_guards: Vec::new(),
906            inherited_held_keys: Arc::new(Vec::new()),
907            task_scopes: Vec::new(),
908            task_counter: 0,
909            runtime_context_counter: 0,
910            runtime_context: crate::runtime_context::RuntimeContext::root(),
911            deadlines: Vec::new(),
912            execution_deadline: super::execution::new_execution_deadline_state(None),
913            breakpoints: BTreeMap::new(),
914            function_breakpoints: std::collections::BTreeSet::new(),
915            pending_function_bp: None,
916            step_mode: false,
917            step_frame_depth: 0,
918            stopped: false,
919            last_line: 0,
920            source_dir: None,
921            package_execution_guard: None,
922            imported_paths: Vec::new(),
923            deferred_cyclic_imports: Vec::new(),
924            module_cache: Arc::new(BTreeMap::new()),
925            prepared_module_cache: crate::PreparedModuleCache::default(),
926            module_provenance: crate::module_artifact::ModuleProvenance::User,
927            module_phase_recorder: None,
928            lazy_callable_modules: Arc::new(crate::value::VmMutex::new(BTreeMap::new())),
929            source_cache: Arc::new(BTreeMap::new()),
930            graph_link_table: None,
931            linked_program_repository: None,
932            source_file: None,
933            source_text: None,
934            coverage: crate::coverage::for_primary(None),
935            bridge: None,
936            denied_builtins: Arc::new(HashSet::new()),
937            cancel_token: None,
938            interrupt_signal_token: None,
939            cancel_grace_instructions_remaining: None,
940            interrupt_handlers: Vec::new(),
941            next_interrupt_handle: 1,
942            pending_interrupt_signal: None,
943            interrupted: false,
944            dispatching_interrupt: false,
945            interrupt_handler_deadline: None,
946            error_stack_trace: Vec::new(),
947            yield_sender: None,
948            project_root: None,
949            globals: Arc::new(crate::value::DictMap::new()),
950            root_harness: None,
951            runtime_effects: crate::orchestration::RuntimeEffectState::fresh(),
952            debug_hook: None,
953            runtime_limits: RuntimeLimits::default(),
954        }
955    }
956
957    pub fn baseline(&self) -> VmBaseline {
958        VmBaseline::from_vm(self)
959    }
960
961    /// Typed, runtime-resolved effects attempted by this VM execution tree.
962    pub fn executed_effects(&self) -> Vec<crate::orchestration::EffectRecord> {
963        self.runtime_effects.snapshot()
964    }
965
966    /// Clear the execution-local effect recorder and per-VM memo.
967    pub fn clear_executed_effects(&mut self) {
968        self.runtime_effects.clear();
969    }
970
971    pub(crate) fn record_capability_effects(
972        &mut self,
973        capability: harn_builtin_meta::CapabilityId,
974        method: &str,
975        args: &[VmValue],
976    ) {
977        self.runtime_effects
978            .record_capability(capability, method, args);
979    }
980
981    pub(crate) fn record_builtin_contract_effects(&mut self, name: &str, args: &[VmValue]) {
982        let Some(entry) = crate::stdlib::recorded_effect_builtin_manifest_entry(name) else {
983            return;
984        };
985        self.record_builtin_effect_specs(entry.contract.effects, args);
986    }
987
988    pub(crate) fn record_builtin_effect_specs(
989        &mut self,
990        specs: &'static [harn_builtin_meta::EffectSpec],
991        args: &[VmValue],
992    ) {
993        self.runtime_effects.record_specs(specs, args);
994    }
995
996    /// Replace the scoped immutable module-template cache used by this VM.
997    /// Fresh runtime module state is still instantiated for every VM.
998    pub fn set_prepared_module_cache(&mut self, cache: crate::PreparedModuleCache) {
999        self.prepared_module_cache = cache;
1000    }
1001
1002    /// Hand this VM the link table for the import graph it is about to run.
1003    ///
1004    /// Only an entry-chunk cache hit can supply one, because only that path holds
1005    /// a manifest proving the recorded digests still describe the files on disk.
1006    /// A table never changes what a module resolves to: one the table does not
1007    /// name, or whose artifact is no longer on disk, is read and compiled as
1008    /// usual.
1009    pub fn set_graph_link_table(
1010        &mut self,
1011        link_table: Option<Arc<crate::context_manifest::GraphLinkTable>>,
1012    ) {
1013        self.graph_link_table = link_table;
1014    }
1015
1016    /// Install the verified module-template repository carried by one linked
1017    /// program. Templates remain scoped to this VM tree and never enter the
1018    /// ordinary caller-independent cache.
1019    pub fn set_linked_program_runtime(
1020        &mut self,
1021        runtime: &crate::linked_program::LinkedProgramRuntime,
1022    ) {
1023        self.linked_program_repository = Some(Arc::clone(&runtime.repository));
1024        self.graph_link_table = None;
1025    }
1026
1027    /// Return the effective runtime limit profile for this VM.
1028    pub fn runtime_limits(&self) -> RuntimeLimits {
1029        self.runtime_limits
1030    }
1031
1032    /// Return a host/debug report describing the VM's effective runtime limits.
1033    pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
1034        self.runtime_limits.report()
1035    }
1036
1037    /// Returns true if any debugging affordance is active — DAP hook,
1038    /// line breakpoints, or function breakpoints. Call-site code uses
1039    /// this to decide whether to capture per-frame restart snapshots
1040    /// (`initial_env`, `initial_local_slots`); without a debugger those
1041    /// snapshots are dead weight, so skipping them removes two
1042    /// allocations from every function call hot path.
1043    ///
1044    /// All three signals are stable across a function call's lifetime
1045    /// (they're set before pipeline execution starts), so the gate is
1046    /// consistent between frame creation and any later `restart_frame`
1047    /// invocation. The three `is_empty` checks compile to a handful of
1048    /// branch-predicted memory probes — cheaper than a single
1049    /// `BTreeMap` clone, which is what we're avoiding.
1050    #[inline]
1051    pub(crate) fn debugger_attached(&self) -> bool {
1052        self.debug_hook.is_some()
1053            || !self.breakpoints.is_empty()
1054            || !self.function_breakpoints.is_empty()
1055    }
1056
1057    /// Set the bridge for delegating unknown builtins in bridge mode.
1058    pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
1059        self.bridge = Some(bridge);
1060    }
1061
1062    /// Set builtins that are denied in sandbox mode.
1063    /// When called, the given builtin names will produce a permission error.
1064    pub fn set_denied_builtins(&mut self, mut denied: HashSet<String>) {
1065        // A builtin may have a private registry name plus one or more public
1066        // aliases. Normalize the whole family once so no dispatch path can
1067        // escape policy merely by using another name for the same handler.
1068        let denied_canonical_names = denied
1069            .iter()
1070            .filter_map(|name| crate::stdlib::builtin_manifest_entry(name))
1071            .map(|entry| entry.canonical_name)
1072            .collect::<HashSet<_>>();
1073        if !denied_canonical_names.is_empty() {
1074            denied.extend(
1075                crate::stdlib::all_builtin_manifest()
1076                    .iter()
1077                    .filter(|entry| denied_canonical_names.contains(entry.canonical_name))
1078                    .map(|entry| entry.name.to_string()),
1079            );
1080        }
1081        self.denied_builtins = Arc::new(denied);
1082    }
1083
1084    /// Set source info for error reporting (file path and source text).
1085    pub fn set_source_info(&mut self, file: &str, text: &str) {
1086        self.source_file = Some(file.to_string());
1087        self.source_text = Some(text.to_string());
1088        if let Some(cov) = self.coverage.as_mut() {
1089            cov.set_primary_file(file);
1090        }
1091        Arc::make_mut(&mut self.source_cache)
1092            .insert(std::path::PathBuf::from(file), Arc::from(text));
1093    }
1094
1095    /// Initialize execution (push the initial frame).
1096    pub fn start(&mut self, chunk: &Chunk) -> Result<(), VmError> {
1097        self.ensure_execution_available()?;
1098        // The top-level pipeline frame captures env at start so
1099        // restartFrame on the outermost frame rewinds to the
1100        // pre-pipeline state — basically "restart session" in
1101        // debugger terms. Skipped when no debugger is attached:
1102        // the snapshot is dead weight in that case and dominates
1103        // call-overhead bench numbers (~5-10%).
1104        let debugger = self.debugger_attached();
1105        let initial_env = if debugger {
1106            Some(self.env.clone())
1107        } else {
1108            None
1109        };
1110        let initial_local_slots = if debugger {
1111            Some(Self::fresh_local_slots(chunk))
1112        } else {
1113            None
1114        };
1115        let chunk = Arc::new(chunk.clone());
1116        let local_slots = Self::fresh_local_slots(&chunk);
1117        let inline_cache_set = self.inline_cache_set_index_for_chunk(&chunk);
1118        self.frames.push(CallFrame {
1119            chunk,
1120            inline_cache_set,
1121            ip: 0,
1122            stack_base: self.stack.len(),
1123            saved_env: self.env.clone(),
1124            initial_env,
1125            initial_local_slots,
1126            saved_iterator_depth: self.iterators.len(),
1127            fn_name: crate::value::HarnStr::new(),
1128            argc: 0,
1129            saved_source_dir: None,
1130            module_functions: None,
1131            module_state: None,
1132            local_slots,
1133            local_scope_base: self.env.scope_depth().saturating_sub(1),
1134            local_scope_depth: 0,
1135        });
1136        Ok(())
1137    }
1138
1139    /// Create a child VM that shares builtins and env but has fresh execution state.
1140    /// Used for parallel/spawn to fork the VM for concurrent tasks.
1141    pub(crate) fn child_vm(&self) -> Vm {
1142        Vm {
1143            stack: Vec::with_capacity(64),
1144            env: self.env.clone(),
1145            output: String::new(),
1146            builtins: Arc::clone(&self.builtins),
1147            async_builtins: Arc::clone(&self.async_builtins),
1148            capability_methods: Arc::clone(&self.capability_methods),
1149            builtin_metadata: Arc::clone(&self.builtin_metadata),
1150            builtins_by_id: Arc::clone(&self.builtins_by_id),
1151            builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
1152            iterators: Vec::new(),
1153            frames: Vec::new(),
1154            exception_handlers: Vec::new(),
1155            spawned_tasks: BTreeMap::new(),
1156            process_exit_request: Arc::clone(&self.process_exit_request),
1157            sync_runtime: self.sync_runtime.clone(),
1158            shared_state_runtime: self.shared_state_runtime.clone(),
1159            inline_cache_sets: Vec::new(),
1160            inline_cache_set_by_chunk: HashMap::new(),
1161            pool_registry: self.pool_registry.clone(),
1162            llm_mock_context: self.llm_mock_context.clone(),
1163            package_snapshot_registry: self.package_snapshot_registry.clone(),
1164            wait_for_graph: self.wait_for_graph.clone(),
1165            held_sync_guards: Vec::new(),
1166            inherited_held_keys: Arc::new(Vec::new()),
1167            task_scopes: Vec::new(),
1168            task_counter: 0,
1169            runtime_context_counter: self.runtime_context_counter,
1170            runtime_context: self.runtime_context.clone(),
1171            deadlines: self.deadlines.clone(),
1172            execution_deadline: self.execution_deadline.fork(),
1173            breakpoints: BTreeMap::new(),
1174            function_breakpoints: std::collections::BTreeSet::new(),
1175            pending_function_bp: None,
1176            step_mode: false,
1177            step_frame_depth: 0,
1178            stopped: false,
1179            last_line: 0,
1180            source_dir: self.source_dir.clone(),
1181            package_execution_guard: self.package_execution_guard.clone(),
1182            imported_paths: Vec::new(),
1183            deferred_cyclic_imports: Vec::new(),
1184            module_cache: Arc::clone(&self.module_cache),
1185            prepared_module_cache: self.prepared_module_cache.clone(),
1186            module_provenance: self.module_provenance,
1187            module_phase_recorder: self.module_phase_recorder.clone(),
1188            lazy_callable_modules: Arc::clone(&self.lazy_callable_modules),
1189            source_cache: Arc::clone(&self.source_cache),
1190            graph_link_table: self.graph_link_table.clone(),
1191            linked_program_repository: self.linked_program_repository.clone(),
1192            source_file: self.source_file.clone(),
1193            source_text: self.source_text.clone(),
1194            coverage: crate::coverage::for_primary(self.source_file.as_deref()),
1195            bridge: self.bridge.clone(),
1196            denied_builtins: Arc::clone(&self.denied_builtins),
1197            cancel_token: self.cancel_token.clone(),
1198            interrupt_signal_token: self.interrupt_signal_token.clone(),
1199            cancel_grace_instructions_remaining: None,
1200            interrupt_handlers: Vec::new(),
1201            next_interrupt_handle: 1,
1202            pending_interrupt_signal: None,
1203            interrupted: self.interrupted,
1204            dispatching_interrupt: false,
1205            interrupt_handler_deadline: None,
1206            error_stack_trace: Vec::new(),
1207            yield_sender: None,
1208            project_root: self.project_root.clone(),
1209            globals: Arc::clone(&self.globals),
1210            root_harness: self.root_harness.clone(),
1211            runtime_effects: crate::orchestration::RuntimeEffectState::with_shared_recorder(
1212                Arc::clone(&self.runtime_effects.recorder),
1213            ),
1214            debug_hook: None,
1215            runtime_limits: self.runtime_limits,
1216        }
1217    }
1218
1219    /// Create a child VM for external adapters that need to invoke Harn
1220    /// closures while sharing the parent's builtins, globals, and module state.
1221    pub(crate) fn child_vm_for_host(&self) -> Vm {
1222        self.child_vm()
1223    }
1224
1225    /// Return the cooperative cancellation/deadline sources that a blocking
1226    /// host adapter must install on its worker thread. The values are cloned
1227    /// once at the async-boundary; the token remains shared with the owning VM
1228    /// so `parallel` aborts and host cancellation reach an in-flight wait.
1229    pub(crate) fn interrupt_sources(
1230        &self,
1231    ) -> (
1232        Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
1233        Option<std::time::Instant>,
1234    ) {
1235        let scope_deadline = self.deadlines.last().map(|(deadline, _)| *deadline);
1236        let deadline = match (scope_deadline, self.interrupt_handler_deadline) {
1237            (Some(scope), Some(interrupt)) => Some(scope.min(interrupt)),
1238            (scope, interrupt) => scope.or(interrupt),
1239        };
1240        (self.cancel_token.clone(), deadline)
1241    }
1242
1243    pub(crate) fn request_process_exit(&self, code: i32) {
1244        self.process_exit_request.request(code);
1245    }
1246
1247    pub(crate) fn requested_process_exit(&self) -> Option<i32> {
1248        self.process_exit_request.code()
1249    }
1250
1251    /// Request cancellation for every outstanding child task owned by this VM
1252    /// and then abort the join handles. This prevents un-awaited spawned tasks
1253    /// from outliving their parent execution scope.
1254    pub(crate) fn cancel_spawned_tasks(&mut self) {
1255        for (_, task) in std::mem::take(&mut self.spawned_tasks) {
1256            task.cancel_token
1257                .store(true, std::sync::atomic::Ordering::SeqCst);
1258            task.handle.abort();
1259        }
1260    }
1261
1262    /// Set the source directory for import resolution and introspection.
1263    /// Also auto-detects the project root if not already set.
1264    pub fn set_source_dir(&mut self, dir: &std::path::Path) {
1265        let dir = crate::stdlib::process::normalize_context_path(dir);
1266        self.source_dir = Some(dir.clone());
1267        crate::stdlib::set_thread_source_dir(&dir);
1268        // Auto-detect project root if not explicitly set.
1269        if self.project_root.is_none() {
1270            self.project_root = crate::stdlib::process::find_project_root(&dir);
1271        }
1272    }
1273
1274    /// Explicitly set the project root directory.
1275    /// Used by ACP/CLI to override auto-detection.
1276    pub fn set_project_root(&mut self, root: &std::path::Path) {
1277        self.project_root = Some(root.to_path_buf());
1278    }
1279
1280    /// Get only the explicit or auto-detected project root, without falling
1281    /// back to `source_dir`.
1282    pub(crate) fn explicit_project_root(&self) -> Option<&std::path::Path> {
1283        self.project_root.as_deref()
1284    }
1285
1286    /// Get the project root directory, falling back to source_dir.
1287    pub fn project_root(&self) -> Option<&std::path::Path> {
1288        self.project_root.as_deref().or(self.source_dir.as_deref())
1289    }
1290
1291    /// Set a global constant (e.g. `pi`, `e`).
1292    /// Stored separately from the environment so user-defined variables can shadow them.
1293    pub fn set_global(&mut self, name: &str, value: VmValue) {
1294        Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
1295    }
1296
1297    /// Read a previously-installed ordinary global constant.
1298    pub fn global(&self, name: &str) -> Option<&VmValue> {
1299        self.globals.get(name)
1300    }
1301
1302    /// Install the script's root authority for the compiler-generated
1303    /// `main(harness: Harness)` call. It is kept out of all source-visible
1304    /// namespaces.
1305    pub fn set_harness(&mut self, harness: crate::harness::Harness) {
1306        self.root_harness = Some(harness.into_vm_value());
1307    }
1308
1309    pub(crate) fn harness(&self) -> Option<&crate::harness::VmHarness> {
1310        match self.root_harness.as_ref() {
1311            Some(VmValue::Harness(handle)) => Some(handle),
1312            _ => None,
1313        }
1314    }
1315
1316    /// Clone the embedder-owned root authority for an explicit exported
1317    /// function parameter. This does not install a source-visible binding.
1318    pub fn root_harness_value(&self) -> Option<VmValue> {
1319        self.root_harness.clone()
1320    }
1321
1322    /// Get the captured output.
1323    pub fn output(&self) -> &str {
1324        &self.output
1325    }
1326
1327    /// Drain and return the captured output, leaving the buffer empty.
1328    /// Used by the async-builtin dispatch path to forward closure output
1329    /// from a child VM back to its parent.
1330    pub fn take_output(&mut self) -> String {
1331        std::mem::take(&mut self.output)
1332    }
1333
1334    /// Append text to this VM's captured output. Used to forward output
1335    /// from child VMs (e.g. closures invoked via `call_closure_pub`)
1336    /// back into the parent stream.
1337    pub fn append_output(&mut self, text: &str) {
1338        self.output.push_str(text);
1339    }
1340
1341    pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
1342        self.stack.pop().ok_or(VmError::StackUnderflow)
1343    }
1344
1345    pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
1346        self.stack.last().ok_or(VmError::StackUnderflow)
1347    }
1348
1349    pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
1350        match c {
1351            Constant::String(s) => Ok(s.as_str()),
1352            _ => Err(VmError::TypeError("expected string constant".into())),
1353        }
1354    }
1355
1356    pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
1357        let depth = self.env.scope_depth();
1358        self.held_sync_guards
1359            .retain(|guard| guard.env_scope_depth < depth);
1360        // A `scope { }` torn down without a normal `TaskScopeExit` (break /
1361        // continue out of it) leaves a dangling nursery — cancel its tasks.
1362        self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
1363    }
1364
1365    pub(crate) fn release_sync_guards_after_unwind(
1366        &mut self,
1367        frame_depth: usize,
1368        env_scope_depth: usize,
1369    ) {
1370        self.held_sync_guards.retain(|guard| {
1371            guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
1372        });
1373        // Cancel nurseries opened above the catch handler (a `throw` unwound
1374        // past their `TaskScopeExit`).
1375        self.cancel_task_scopes_where(|s| {
1376            !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
1377        });
1378    }
1379
1380    pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
1381        self.held_sync_guards
1382            .retain(|guard| guard.frame_depth != frame_depth);
1383        // Cancel any nursery whose `scope {}` block belonged to the frame being
1384        // torn down (e.g. a `return` jumped past its `TaskScopeExit`).
1385        self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
1386    }
1387
1388    pub(crate) fn adopt_sync_permit_for_current_scope(
1389        &mut self,
1390        permit: crate::value::VmSyncPermitHandle,
1391    ) {
1392        if permit.is_released()
1393            || self
1394                .held_sync_guards
1395                .iter()
1396                .any(|guard| guard._permit.same_lease(&permit))
1397        {
1398            return;
1399        }
1400        self.held_sync_guards
1401            .push(crate::synchronization::VmSyncHeldGuard {
1402                _permit: permit,
1403                frame_depth: self.frames.len(),
1404                env_scope_depth: self.env.scope_depth(),
1405            });
1406    }
1407
1408    /// Deregister a task id from every open nursery (it was explicitly
1409    /// `await`ed, so it must not be double-joined or cancelled at scope exit).
1410    pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
1411        for scope in &mut self.task_scopes {
1412            scope.task_ids.retain(|t| t != id);
1413        }
1414    }
1415
1416    /// Cancel and remove every task scope matching `doomed`, aborting its bound
1417    /// tasks (used when a `scope {}` is torn down without a normal join).
1418    fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
1419        let mut i = 0;
1420        while i < self.task_scopes.len() {
1421            if doomed(&self.task_scopes[i]) {
1422                let scope = self.task_scopes.remove(i);
1423                for id in &scope.task_ids {
1424                    if let Some(task) = self.spawned_tasks.remove(id) {
1425                        task.cancel_token
1426                            .store(true, std::sync::atomic::Ordering::SeqCst);
1427                        task.handle.abort();
1428                    }
1429                }
1430            } else {
1431                i += 1;
1432            }
1433        }
1434    }
1435
1436    /// Total live permits this VM already holds for `kind:key`. The held-set is
1437    /// tiny (bounded by lexical nesting and explicit sync acquisitions), so this
1438    /// scan is cheap and only runs on the rare blocking-acquire path.
1439    pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
1440        let own: u32 = self
1441            .held_sync_guards
1442            .iter()
1443            .filter(|guard| {
1444                !guard._permit.is_released()
1445                    && guard._permit.kind() == kind
1446                    && guard._permit.key() == key
1447            })
1448            .map(|guard| guard._permit.permits())
1449            .sum();
1450        let inherited: u32 = self
1451            .inherited_held_keys
1452            .iter()
1453            .filter(|held| held.kind == kind && held.key == key)
1454            .map(|held| held.permits)
1455            .sum();
1456        own + inherited
1457    }
1458
1459    /// Every live sync permit held by this VM *and* its suspended ancestors: the
1460    /// transitive held-set seen by an inline child.
1461    pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1462        let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1463            .held_sync_guards
1464            .iter()
1465            .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1466            .collect();
1467        keys.extend(self.inherited_held_keys.iter().cloned());
1468        keys
1469    }
1470
1471    /// Clone a child VM for an **inline, same-task** execution (an async builtin
1472    /// awaited while this VM is parked, or a user closure that builtin runs and
1473    /// awaits). The child inherits this VM's transitive held-lock keys so a
1474    /// re-acquire of a parent-held lock is caught as a self-deadlock
1475    /// (HARN-ORC-011). Use plain `child_vm()` for new concurrent tasks.
1476    pub(crate) fn child_vm_inline(&self) -> Vm {
1477        let mut child = self.child_vm();
1478        child.inherited_held_keys = Arc::new(self.combined_held_keys());
1479        child
1480    }
1481}
1482
1483impl Drop for Vm {
1484    fn drop(&mut self) {
1485        if let Some(coverage) = self.coverage.take() {
1486            crate::coverage::merge_into_global(coverage);
1487        }
1488        self.cancel_spawned_tasks();
1489    }
1490}
1491
1492impl Default for Vm {
1493    fn default() -> Self {
1494        Self::new()
1495    }
1496}
1497
1498#[cfg(test)]
1499#[path = "state_tests.rs"]
1500mod tests;