Skip to main content

harn_vm/vm/
state.rs

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