1use std::sync::Arc;
2use std::time::{Duration, Instant};
3
4use crate::chunk::{Chunk, ChunkRef, Op};
5use crate::value::{ModuleFunctionRegistry, VmError, VmValue};
6
7use super::callable_entry::TopLevelEntry;
8use super::state::ExecutionDeadlineState;
9use super::{CallFrame, LocalSlot, Vm};
10
11const CANCEL_GRACE_ASYNC_OP: Duration = Duration::from_millis(250);
12
13pub(super) fn new_execution_deadline_state(
14 deadline: Option<Instant>,
15) -> Arc<ExecutionDeadlineState> {
16 ExecutionDeadlineState::new(Instant::now(), deadline)
17}
18
19#[cfg(test)]
20thread_local! {
21 static SCOPE_INTERRUPT_ASYNC_DISPATCHES: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
22}
23
24#[cfg(test)]
25pub(super) fn reset_scope_interrupt_async_dispatches() {
26 SCOPE_INTERRUPT_ASYNC_DISPATCHES.set(0);
27}
28
29#[cfg(test)]
30pub(super) fn scope_interrupt_async_dispatches() -> u64 {
31 SCOPE_INTERRUPT_ASYNC_DISPATCHES.get()
32}
33
34#[derive(Clone, Copy)]
35enum DeadlineKind {
36 Execution,
37 Scope,
38 InterruptHandler,
39}
40
41impl Vm {
42 #[inline]
49 pub(crate) fn scope_interrupts_clean(&self) -> bool {
50 self.requested_process_exit().is_none()
51 && self.cancel_token.is_none()
52 && self.interrupt_signal_token.is_none()
53 && self.pending_interrupt_signal.is_none()
54 && self.interrupt_handler_deadline.is_none()
55 && !self.execution_deadline.is_active()
56 && self.deadlines.is_empty()
57 }
58
59 pub async fn execute(&mut self, chunk: &Chunk) -> Result<VmValue, VmError> {
68 self.execute_arc(Arc::new(chunk.clone())).await
69 }
70
71 pub async fn execute_with_timeout(
82 &mut self,
83 chunk: &Chunk,
84 timeout: Duration,
85 ) -> Result<VmValue, VmError> {
86 self.execute_top_level_with_timeout(TopLevelEntry::Chunk(Arc::new(chunk.clone())), timeout)
87 .await
88 }
89
90 pub(super) async fn execute_top_level_with_timeout(
91 &mut self,
92 entry: TopLevelEntry,
93 timeout: Duration,
94 ) -> Result<VmValue, VmError> {
95 let deadline = Instant::now().checked_add(timeout).ok_or_else(|| {
96 VmError::Runtime("execution timeout exceeds the platform clock range".to_string())
97 })?;
98 crate::orchestration::scope_ambient_transaction(async {
99 let pipeline_checkpoint = crate::orchestration::checkpoint_pipeline_lifecycle();
100 let deadline_guard = self.execution_deadline.install(deadline);
101 let result = crate::tracing::checkpoint_future(self.execute_top_level(entry)).await;
102 deadline_guard.complete();
103 pipeline_checkpoint.complete();
104 result
105 })
106 .await
107 }
108
109 pub async fn execute_arc(&mut self, chunk: ChunkRef) -> Result<VmValue, VmError> {
115 self.execute_top_level(TopLevelEntry::Chunk(chunk)).await
116 }
117
118 pub(super) async fn run_pipeline_finish_lifecycle(
125 &mut self,
126 value: VmValue,
127 ) -> Result<VmValue, VmError> {
128 use crate::orchestration::{
129 take_pipeline_on_finish, unsettled_state_snapshot_async, HookEvent,
130 };
131 let _tape_phase =
132 crate::testbench::tape::enter_phase(crate::testbench::tape::TapePhase::RuntimeFinalize);
133
134 let on_finish = take_pipeline_on_finish();
135 let unsettled = unsettled_state_snapshot_async().await;
136
137 let pre_payload = serde_json::json!({
138 "event": HookEvent::PreFinish.as_str(),
139 "return_value": crate::llm::vm_value_to_json(&value),
140 "unsettled": unsettled.to_json(),
141 "has_on_finish": on_finish.is_some(),
142 });
143 self.fire_finish_lifecycle_event(HookEvent::PreFinish, &pre_payload)
144 .await?;
145
146 if !unsettled.is_empty() {
147 let payload = serde_json::json!({
148 "event": HookEvent::OnUnsettledDetected.as_str(),
149 "unsettled": unsettled.to_json(),
150 });
151 self.fire_finish_lifecycle_event(HookEvent::OnUnsettledDetected, &payload)
152 .await?;
153 }
154
155 let final_value = if let Some(closure) = on_finish {
156 let harness_value = self.root_harness_value().ok_or_else(|| {
157 VmError::Runtime(
158 "pipeline finish callback requires Harness, but no root Harness is installed"
159 .to_string(),
160 )
161 })?;
162 self.call_closure_pub(&closure, &[harness_value, value])
163 .await?
164 } else {
165 value
166 };
167
168 let post_payload = serde_json::json!({
169 "event": HookEvent::PostFinish.as_str(),
170 "return_value": crate::llm::vm_value_to_json(&final_value),
171 "unsettled": unsettled.to_json(),
172 });
173 self.fire_finish_lifecycle_event(HookEvent::PostFinish, &post_payload)
174 .await?;
175
176 Ok(final_value)
177 }
178
179 async fn fire_finish_lifecycle_event(
197 &mut self,
198 event: crate::orchestration::HookEvent,
199 payload: &serde_json::Value,
200 ) -> Result<(), VmError> {
201 use crate::orchestration::{HookControl, HookEvent};
202 let invocations = crate::orchestration::matching_vm_lifecycle_hooks(event, payload);
203 if invocations.is_empty() {
204 return Ok(());
205 }
206 let harness = self.root_harness_value().ok_or_else(|| {
207 VmError::Runtime(
208 "pipeline lifecycle hook requires Harness, but no root Harness is installed"
209 .to_string(),
210 )
211 })?;
212 let mut current_payload = payload.clone();
213 for invocation in invocations {
214 let arg = crate::stdlib::json_to_vm_value(¤t_payload);
215 let closure = invocation.resolve(self).await?;
216 let raw = self
217 .call_closure_pub(&closure, &[harness.clone(), arg])
218 .await?;
219 let (action, effects) = crate::orchestration::collect_hook_effects_and_action(
220 event,
221 raw,
222 crate::value::VmValue::Nil,
223 )?;
224 crate::orchestration::inject_hook_effects_into_current_session(effects)?;
225 let control = crate::orchestration::parse_hook_control_for_finish(event, &action)?;
226 match control {
227 HookControl::Allow => {}
228 HookControl::Block { reason } => {
229 if matches!(event, HookEvent::PreFinish) {
230 return Err(VmError::Runtime(format!(
231 "PreFinish hook returned block, which is not a valid control: {reason}. \
232 To delay pipeline finish until unsettled work clears, use \
233 OnFinish.block_until_settled (std/lifecycle) or return Modify/Allow \
234 from PreFinish."
235 )));
236 }
237 if matches!(event, HookEvent::PostFinish) {
238 continue;
240 }
241 return Err(VmError::Runtime(format!(
243 "{} hook blocked pipeline finish: {reason}",
244 event.as_str()
245 )));
246 }
247 HookControl::Modify { payload: modified } => {
248 current_payload = modified;
249 }
250 HookControl::Decision { .. } => {}
251 }
252 }
253 Ok(())
254 }
255
256 pub(crate) fn handle_error(&mut self, error: VmError) -> Result<Option<VmValue>, VmError> {
258 if let Some(code) = error.process_exit_code() {
259 self.request_process_exit(code);
260 }
261 if error.is_uncatchable_control_flow() {
262 return Err(error);
263 }
264 let thrown_value = error.thrown_value();
265
266 if let Some(handler) = self.exception_handlers.pop() {
267 if let Some(error_type) = handler.error_type.as_deref() {
268 let matches = match &thrown_value {
270 VmValue::EnumVariant(enum_variant) => enum_variant.has_enum_name(error_type),
271 _ => false,
272 };
273 if !matches {
274 return self.handle_error(error);
275 }
276 }
277
278 self.release_sync_guards_after_unwind(handler.frame_depth, handler.env_scope_depth);
279
280 while self.frames.len() > handler.frame_depth {
281 if let Some(frame) = self.frames.pop() {
282 if let Some(ref dir) = frame.saved_source_dir {
283 crate::stdlib::set_thread_source_dir(dir);
284 }
285 self.iterators.truncate(frame.saved_iterator_depth);
286 self.env = frame.saved_env;
287 }
288 }
289 crate::step_runtime::prune_below_frame(self.frames.len());
290
291 while self
293 .deadlines
294 .last()
295 .is_some_and(|d| d.1 > handler.frame_depth)
296 {
297 self.deadlines.pop();
298 }
299
300 self.env.truncate_scopes(handler.env_scope_depth);
301
302 self.stack.truncate(handler.stack_depth);
303 self.stack.push(thrown_value);
304
305 if let Some(frame) = self.frames.last_mut() {
306 frame.ip = handler.catch_ip;
307 }
308
309 Ok(None)
310 } else {
311 Err(error)
312 }
313 }
314
315 pub(crate) async fn run_chunk(&mut self, chunk: ChunkRef) -> Result<VmValue, VmError> {
316 self.run_chunk_ref(chunk, 0, None, None, None, None).await
317 }
318
319 pub(crate) async fn run_chunk_ref(
320 &mut self,
321 chunk: ChunkRef,
322 argc: usize,
323 saved_source_dir: Option<std::path::PathBuf>,
324 module_functions: Option<ModuleFunctionRegistry>,
325 module_state: Option<crate::value::ModuleState>,
326 local_slots: Option<Vec<LocalSlot>>,
327 ) -> Result<VmValue, VmError> {
328 self.ensure_execution_available()?;
329 let debugger = self.debugger_attached();
330 let local_slots = local_slots.unwrap_or_else(|| Self::fresh_local_slots(&chunk));
331 let initial_env = if debugger {
332 Some(self.env.clone())
333 } else {
334 None
335 };
336 let initial_local_slots = if debugger {
337 Some(local_slots.clone())
338 } else {
339 None
340 };
341 let inline_cache_set = self.inline_cache_set_index_for_chunk(&chunk);
342 self.frames.push(CallFrame {
343 chunk,
344 inline_cache_set,
345 ip: 0,
346 stack_base: self.stack.len(),
347 saved_env: self.env.clone(),
348 initial_env,
349 initial_local_slots,
350 saved_iterator_depth: self.iterators.len(),
351 fn_name: crate::value::HarnStr::new(),
352 argc,
353 saved_source_dir,
354 module_functions,
355 module_state,
356 local_slots,
357 local_scope_base: self.env.scope_depth().saturating_sub(1),
358 local_scope_depth: 0,
359 });
360
361 self.drive_dispatch_loop(0, false).await
362 }
363
364 pub(crate) async fn drive_until_frame_depth(
371 &mut self,
372 target_depth: usize,
373 ) -> Result<VmValue, VmError> {
374 self.drive_dispatch_loop(target_depth, true).await
375 }
376
377 async fn drive_dispatch_loop(
391 &mut self,
392 target_depth: usize,
393 restore_on_final_pop: bool,
394 ) -> Result<VmValue, VmError> {
395 self.ensure_execution_available()?;
396 let _task_activity = self
397 .wait_for_graph
398 .register_task(self.runtime_context.task_id.clone());
399 loop {
400 if !self.scope_interrupts_clean() {
407 if let Some(err) = self.pending_scope_interrupt().await {
408 match self.handle_error(err) {
409 Ok(None) => continue,
410 Ok(Some(val)) => return Ok(val),
411 Err(e) => {
412 self.unwind_frames_to_depth(target_depth);
413 return Err(e);
414 }
415 }
416 }
417 }
418
419 let frame = match self.frames.last_mut() {
420 Some(f) => f,
421 None => return Ok(self.stack.pop().unwrap_or(VmValue::Nil)),
422 };
423
424 if frame.ip >= frame.chunk.code.len() {
425 let val = self.stack.pop().unwrap_or(VmValue::Nil);
426 let val = self.run_step_post_hooks_for_current_frame(val).await?;
427 self.release_sync_guards_for_frame(self.frames.len());
428 let popped_frame = self.frames.pop().unwrap();
429 if let Some(ref dir) = popped_frame.saved_source_dir {
430 crate::stdlib::set_thread_source_dir(dir);
431 }
432 let current_depth = self.frames.len();
433 crate::step_runtime::prune_below_frame(current_depth);
434 while self.deadlines.last().is_some_and(|d| d.1 > current_depth) {
439 self.deadlines.pop();
440 }
441
442 let reached_target = current_depth <= target_depth;
443 if reached_target && !restore_on_final_pop {
444 return Ok(val);
447 }
448 self.iterators.truncate(popped_frame.saved_iterator_depth);
449 self.env = popped_frame.saved_env;
450 self.stack.truncate(popped_frame.stack_base);
451 if reached_target {
452 return Ok(val);
453 }
454 self.stack.push(val);
455 continue;
456 }
457
458 let op_byte = frame.chunk.code[frame.ip];
459 if let Some(coverage) = self.coverage.as_mut() {
465 coverage.record(&frame.chunk, frame.ip);
466 }
467 frame.ip += 1;
468
469 let op = match Op::from_byte(op_byte) {
474 Some(op) => op,
475 None => return Err(VmError::InvalidInstruction(op_byte)),
476 };
477 let op_result: Result<(), VmError> = if let Some(result) = self.execute_op_sync(op) {
478 result
479 } else if self.scope_interrupts_clean() {
480 self.execute_op_async(op).await
481 } else {
482 match self.execute_op_with_scope_interrupts(op_byte).await {
483 Ok(Some(val)) => return Ok(val),
484 Ok(None) => Ok(()),
485 Err(e) => Err(e),
486 }
487 };
488
489 match op_result {
490 Ok(()) => continue,
491 Err(VmError::Return(val)) => {
492 let val = self.run_step_post_hooks_for_current_frame(val).await?;
493 if let Some(popped_frame) = self.frames.pop() {
494 self.release_sync_guards_for_frame(self.frames.len() + 1);
495 if let Some(ref dir) = popped_frame.saved_source_dir {
496 crate::stdlib::set_thread_source_dir(dir);
497 }
498 let current_depth = self.frames.len();
499 self.exception_handlers
500 .retain(|h| h.frame_depth <= current_depth);
501 crate::step_runtime::prune_below_frame(current_depth);
502 while self.deadlines.last().is_some_and(|d| d.1 > current_depth) {
503 self.deadlines.pop();
504 }
505
506 let reached_target = current_depth <= target_depth;
507 if reached_target && !restore_on_final_pop {
508 return Ok(val);
509 }
510 self.iterators.truncate(popped_frame.saved_iterator_depth);
511 self.env = popped_frame.saved_env;
512 self.stack.truncate(popped_frame.stack_base);
513 if reached_target {
514 return Ok(val);
515 }
516 self.stack.push(val);
517 } else {
518 return Ok(val);
519 }
520 }
521 Err(e) => {
522 if self.error_stack_trace.is_empty() {
524 self.error_stack_trace = self.capture_stack_trace();
525 }
526 let e = match self.apply_step_error_boundary(e) {
533 StepBoundaryOutcome::Returned(val) => {
534 self.error_stack_trace.clear();
535 if self.frames.len() <= target_depth {
536 return Ok(val);
537 }
538 self.stack.push(val);
539 continue;
540 }
541 StepBoundaryOutcome::Throw(err) => err,
542 };
543 match self.handle_error(e) {
544 Ok(None) => {
545 self.error_stack_trace.clear();
546 continue;
547 }
548 Ok(Some(val)) => return Ok(val),
549 Err(e) => {
550 self.unwind_frames_to_depth(target_depth);
551 return Err(self.enrich_error_with_line(e));
552 }
553 }
554 }
555 }
556 }
557 }
558
559 fn unwind_frames_to_depth(&mut self, target_depth: usize) {
566 while self.frames.len() > target_depth {
567 let frame_depth = self.frames.len();
568 if let Some(frame) = self.frames.pop() {
569 self.release_sync_guards_for_frame(frame_depth);
570 if let Some(ref dir) = frame.saved_source_dir {
571 crate::stdlib::set_thread_source_dir(dir);
572 }
573 self.iterators.truncate(frame.saved_iterator_depth);
574 self.env = frame.saved_env;
575 self.stack.truncate(frame.stack_base);
576 }
577 }
578 let current_depth = self.frames.len();
579 crate::step_runtime::prune_below_frame(current_depth);
580 while self.deadlines.last().is_some_and(|d| d.1 > current_depth) {
581 self.deadlines.pop();
582 }
583 }
584
585 pub(crate) fn apply_step_error_boundary(&mut self, error: VmError) -> StepBoundaryOutcome {
591 use crate::step_runtime;
592 if !step_runtime::is_step_budget_exhausted(&error) {
593 return StepBoundaryOutcome::Throw(error);
594 }
595 let Some(step_depth) = step_runtime::active_step_frame_depth() else {
596 return StepBoundaryOutcome::Throw(error);
597 };
598 if step_depth != self.frames.len() {
603 return StepBoundaryOutcome::Throw(error);
604 }
605 let boundary = step_runtime::with_active_step(|step| step.definition.boundary())
606 .unwrap_or(step_runtime::StepErrorBoundary::Fail);
607 match boundary {
608 step_runtime::StepErrorBoundary::Continue => {
609 if let Some(popped) = self.frames.pop() {
613 self.release_sync_guards_for_frame(self.frames.len() + 1);
614 if let Some(ref dir) = popped.saved_source_dir {
615 crate::stdlib::set_thread_source_dir(dir);
616 }
617 let current_depth = self.frames.len();
618 self.exception_handlers
619 .retain(|h| h.frame_depth <= current_depth);
620 step_runtime::pop_and_record(
621 current_depth + 1,
622 "skipped",
623 Some(step_runtime_error_message(&error)),
624 );
625 if self.frames.is_empty() {
626 return StepBoundaryOutcome::Returned(VmValue::Nil);
627 }
628 self.iterators.truncate(popped.saved_iterator_depth);
629 self.env = popped.saved_env;
630 self.stack.truncate(popped.stack_base);
631 }
632 StepBoundaryOutcome::Returned(VmValue::Nil)
633 }
634 step_runtime::StepErrorBoundary::Escalate => {
635 let identity = step_runtime::with_active_step(|step| {
636 (
637 step.definition.name.clone(),
638 step.definition.function.clone(),
639 )
640 });
641 step_runtime::pop_and_record(
642 step_depth,
643 "escalated",
644 Some(step_runtime_error_message(&error)),
645 );
646 let (step_name, function) = identity.unzip();
647 StepBoundaryOutcome::Throw(step_runtime::mark_escalated(
648 error,
649 step_name.as_deref(),
650 function.as_deref(),
651 ))
652 }
653 step_runtime::StepErrorBoundary::Fail => {
654 step_runtime::pop_and_record(
655 step_depth,
656 "failed",
657 Some(step_runtime_error_message(&error)),
658 );
659 StepBoundaryOutcome::Throw(error)
660 }
661 }
662 }
663}
664
665fn next_deadline(
666 execution_deadline: Option<Instant>,
667 scope_deadline: Option<Instant>,
668 interrupt_handler_deadline: Option<Instant>,
669) -> (Option<Instant>, Option<DeadlineKind>) {
670 [
671 (execution_deadline, DeadlineKind::Execution),
672 (scope_deadline, DeadlineKind::Scope),
673 (interrupt_handler_deadline, DeadlineKind::InterruptHandler),
674 ]
675 .into_iter()
676 .filter_map(|(deadline, kind)| deadline.map(|deadline| (deadline, kind)))
677 .min_by_key(|(deadline, _)| *deadline)
678 .map_or((None, None), |(deadline, kind)| {
679 (Some(deadline), Some(kind))
680 })
681}
682
683fn step_runtime_error_message(error: &VmError) -> String {
684 match error {
685 VmError::Thrown(VmValue::Dict(dict)) => dict
686 .get("message")
687 .map(|v| v.display())
688 .unwrap_or_else(|| error.to_string()),
689 _ => error.to_string(),
690 }
691}
692
693pub(crate) enum StepBoundaryOutcome {
694 Returned(VmValue),
695 Throw(VmError),
696}
697
698impl crate::vm::Vm {
699 pub(crate) async fn execute_one_cycle(&mut self) -> Result<Option<(VmValue, bool)>, VmError> {
700 if let Some(err) = self.pending_scope_interrupt().await {
701 match self.handle_error(err) {
702 Ok(None) => return Ok(None),
703 Ok(Some(val)) => return Ok(Some((val, false))),
704 Err(e) => return Err(e),
705 }
706 }
707
708 let frame = match self.frames.last_mut() {
709 Some(f) => f,
710 None => {
711 let val = self.stack.pop().unwrap_or(VmValue::Nil);
712 return Ok(Some((val, false)));
713 }
714 };
715
716 if frame.ip >= frame.chunk.code.len() {
717 let val = self.stack.pop().unwrap_or(VmValue::Nil);
718 self.release_sync_guards_for_frame(self.frames.len());
719 let popped_frame = self.frames.pop().unwrap();
720 if self.frames.is_empty() {
721 return Ok(Some((val, false)));
722 }
723 self.iterators.truncate(popped_frame.saved_iterator_depth);
724 self.env = popped_frame.saved_env;
725 self.stack.truncate(popped_frame.stack_base);
726 self.stack.push(val);
727 return Ok(None);
728 }
729
730 let op = frame.chunk.code[frame.ip];
731 frame.ip += 1;
732
733 match self.execute_op_with_scope_interrupts(op).await {
734 Ok(Some(val)) => Ok(Some((val, false))),
735 Ok(None) => Ok(None),
736 Err(VmError::Return(val)) => {
737 if let Some(popped_frame) = self.frames.pop() {
738 self.release_sync_guards_for_frame(self.frames.len() + 1);
739 if let Some(ref dir) = popped_frame.saved_source_dir {
740 crate::stdlib::set_thread_source_dir(dir);
741 }
742 let current_depth = self.frames.len();
743 self.exception_handlers
744 .retain(|h| h.frame_depth <= current_depth);
745 if self.frames.is_empty() {
746 return Ok(Some((val, false)));
747 }
748 self.iterators.truncate(popped_frame.saved_iterator_depth);
749 self.env = popped_frame.saved_env;
750 self.stack.truncate(popped_frame.stack_base);
751 self.stack.push(val);
752 Ok(None)
753 } else {
754 Ok(Some((val, false)))
755 }
756 }
757 Err(e) => {
758 if self.error_stack_trace.is_empty() {
759 self.error_stack_trace = self.capture_stack_trace();
760 }
761 match self.handle_error(e) {
762 Ok(None) => {
763 self.error_stack_trace.clear();
764 Ok(None)
765 }
766 Ok(Some(val)) => Ok(Some((val, false))),
767 Err(e) => Err(self.enrich_error_with_line(e)),
768 }
769 }
770 }
771 }
772
773 async fn execute_op_with_scope_interrupts(
774 &mut self,
775 op: u8,
776 ) -> Result<Option<VmValue>, VmError> {
777 #[cfg(test)]
778 SCOPE_INTERRUPT_ASYNC_DISPATCHES
779 .set(SCOPE_INTERRUPT_ASYNC_DISPATCHES.get().saturating_add(1));
780
781 enum ScopeInterruptResult {
782 Op(Result<Option<VmValue>, VmError>),
783 Deadline(DeadlineKind),
784 CancelTimedOut,
785 }
786
787 let execution_deadline = Arc::clone(&self.execution_deadline);
788 let scope_deadline = self.deadlines.last().map(|(deadline, _)| *deadline);
789 let interrupt_handler_deadline = self.interrupt_handler_deadline;
790 let cancel_token = self.cancel_token.clone();
791
792 let has_deadline = execution_deadline.is_active()
793 || scope_deadline.is_some()
794 || interrupt_handler_deadline.is_some();
795 if !has_deadline && cancel_token.is_none() {
796 return self.execute_op(op).await;
797 }
798
799 let cancel_requested_at_start = cancel_token
800 .as_ref()
801 .is_some_and(|token| token.load(std::sync::atomic::Ordering::SeqCst));
802 let has_cancel = cancel_token.is_some() && !cancel_requested_at_start;
803 let deadline_sleep = async move {
804 loop {
805 let changed = execution_deadline.changed();
808 let (deadline, kind) = next_deadline(
809 execution_deadline.current(),
810 scope_deadline,
811 interrupt_handler_deadline,
812 );
813 if let Some(deadline) = deadline {
814 tokio::select! {
815 _ = tokio::time::sleep_until(tokio::time::Instant::from_std(deadline)) => {
816 return kind.unwrap_or(DeadlineKind::Scope);
817 }
818 _ = changed => {}
819 }
820 } else {
821 changed.await;
822 }
823 }
824 };
825 let cancel_sleep = async move {
826 if let Some(token) = cancel_token {
827 while !token.load(std::sync::atomic::Ordering::SeqCst) {
828 tokio::time::sleep(Duration::from_millis(10)).await;
829 }
830 } else {
831 std::future::pending::<()>().await;
832 }
833 };
834
835 let result = {
836 let op_future = self.execute_op(op);
837 tokio::pin!(op_future);
838 tokio::select! {
839 result = &mut op_future => ScopeInterruptResult::Op(result),
840 kind = deadline_sleep, if has_deadline => {
841 ScopeInterruptResult::Deadline(kind)
842 },
843 _ = cancel_sleep, if has_cancel => {
844 let grace = tokio::time::sleep(CANCEL_GRACE_ASYNC_OP);
845 tokio::pin!(grace);
846 tokio::select! {
847 result = &mut op_future => ScopeInterruptResult::Op(result),
848 _ = &mut grace => ScopeInterruptResult::CancelTimedOut,
849 }
850 }
851 }
852 };
853
854 match result {
855 ScopeInterruptResult::Op(result) => result,
856 ScopeInterruptResult::Deadline(DeadlineKind::Execution) => {
857 self.cancel_spawned_tasks();
858 Err(VmError::ExecutionDeadlineExceeded)
859 }
860 ScopeInterruptResult::Deadline(DeadlineKind::Scope) => {
861 self.deadlines.pop();
862 self.cancel_spawned_tasks();
863 Err(Self::deadline_exceeded_error())
864 }
865 ScopeInterruptResult::Deadline(DeadlineKind::InterruptHandler) => {
866 Err(Self::interrupt_handler_timeout_error())
867 }
868 ScopeInterruptResult::CancelTimedOut => {
869 self.cancel_spawned_tasks();
870 let signal = self
871 .take_host_interrupt_signal()
872 .unwrap_or_else(|| "SIGINT".to_string());
873 if self.has_interrupt_handler_for(&signal) {
874 self.dispatch_interrupt_handlers(&signal).await?;
875 }
876 Err(Self::cancelled_error())
877 }
878 }
879 }
880
881 pub(crate) fn deadline_exceeded_error() -> VmError {
882 VmError::Thrown(VmValue::String(arcstr::ArcStr::from("Deadline exceeded")))
883 }
884
885 pub(crate) fn cancelled_error() -> VmError {
886 VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
887 "kind:cancelled:VM cancelled by host",
888 )))
889 }
890
891 pub(crate) fn capture_stack_trace(&self) -> Vec<(String, usize, usize, Option<String>)> {
893 self.frames
894 .iter()
895 .map(|f| {
896 let idx = if f.ip > 0 { f.ip - 1 } else { 0 };
897 let line = f.chunk.lines.get(idx).copied().unwrap_or(0) as usize;
898 let col = f.chunk.columns.get(idx).copied().unwrap_or(0) as usize;
899 (
900 f.fn_name.to_string(),
901 line,
902 col,
903 f.chunk.source_file.clone(),
904 )
905 })
906 .collect()
907 }
908
909 pub(crate) fn enrich_error_with_line(&self, error: VmError) -> VmError {
913 let (line, file) = self
918 .error_stack_trace
919 .last()
920 .map(|(_, l, _, f)| (*l, f.clone()))
921 .unwrap_or_else(|| (self.current_line(), None));
922 if line == 0 {
923 return error;
924 }
925 let suffix = match file.as_deref() {
926 Some(path) => {
927 let name = std::path::Path::new(path)
928 .file_name()
929 .and_then(|n| n.to_str())
930 .unwrap_or(path);
931 format!(" ({name}:{line})")
932 }
933 None => format!(" (line {line})"),
934 };
935 match error {
936 VmError::Runtime(msg) => VmError::Runtime(format!("{msg}{suffix}")),
937 VmError::TypeError(msg) => VmError::TypeError(format!("{msg}{suffix}")),
938 VmError::DivisionByZero => VmError::Runtime(format!("Division by zero{suffix}")),
939 VmError::UndefinedVariable(name) => {
940 VmError::Runtime(format!("Undefined variable: {name}{suffix}"))
941 }
942 VmError::UndefinedBuiltin(name) => {
943 VmError::Runtime(format!("Undefined builtin: {name}{suffix}"))
944 }
945 VmError::ImmutableAssignment(name) => VmError::Runtime(format!(
946 "Cannot assign to immutable binding: {name}{suffix}"
947 )),
948 VmError::StackOverflow => {
949 VmError::Runtime(format!("Stack overflow: too many nested calls{suffix}"))
950 }
951 other => other,
957 }
958 }
959}
960
961#[cfg(test)]
962mod tests {
963 use super::*;
964 use crate::compiler::Compiler;
965 use crate::stdlib::register_vm_stdlib;
966 use harn_lexer::Lexer;
967 use harn_parser::Parser;
968 use std::sync::atomic::{AtomicBool, Ordering};
969
970 fn compile_harn(source: &str) -> Chunk {
971 let mut lexer = Lexer::new(source);
972 let tokens = lexer.tokenize().unwrap();
973 let mut parser = Parser::new(tokens);
974 let program = parser.parse().unwrap();
975 Compiler::new().compile(&program).unwrap()
976 }
977
978 #[tokio::test(flavor = "current_thread")]
979 async fn dropping_timed_execution_restores_ambient_state_and_poison_vm_reuse() {
980 let local = tokio::task::LocalSet::new();
981 local
982 .run_until(async {
983 crate::reset_thread_local_state();
984 let baseline_dir = tempfile::tempdir().unwrap();
985 let imported_dir = tempfile::tempdir().unwrap();
986 let poisoned_dir = tempfile::tempdir().unwrap();
987 let quick = compile_harn("pipeline default(harness: Harness) { return 42 }");
988 let mut vm = Vm::new();
989 register_vm_stdlib(&mut vm);
990 crate::tracing::set_tracing_enabled(true);
991
992 let child_started = Arc::new(AtomicBool::new(false));
993 let child_effect = Arc::new(AtomicBool::new(false));
994 let child_release = Arc::new(tokio::sync::Notify::new());
995 let (ambient_started_tx, ambient_started_rx) = tokio::sync::oneshot::channel();
996 let ambient_started_tx = Arc::new(std::sync::Mutex::new(Some(ambient_started_tx)));
997 let started_for_builtin = Arc::clone(&child_started);
998 vm.register_builtin("child_started", move |_args, _output| {
999 started_for_builtin.store(true, Ordering::Release);
1000 Ok(VmValue::Nil)
1001 });
1002 let effect_for_builtin = Arc::clone(&child_effect);
1003 vm.register_builtin("child_effect", move |_args, _output| {
1004 effect_for_builtin.store(true, Ordering::Release);
1005 Ok(VmValue::Nil)
1006 });
1007 let release_for_builtin = Arc::clone(&child_release);
1008 vm.register_async_builtin("wait_for_child_release", move |_ctx, _args| {
1009 let release = Arc::clone(&release_for_builtin);
1010 async move {
1011 release.notified().await;
1012 Ok(VmValue::Nil)
1013 }
1014 });
1015 vm.register_async_builtin("wait_forever", |_ctx, _args| async move {
1016 std::future::pending::<()>().await;
1017 Ok(VmValue::Nil)
1018 });
1019
1020 let imported_path = imported_dir.path().join("cancelled.harn");
1021 let imported_source_dir = imported_dir.path().to_path_buf();
1022 let poisoned_source_dir = poisoned_dir.path().to_path_buf();
1023 let ambient_started_for_builtin = Arc::clone(&ambient_started_tx);
1024 vm.register_builtin("strand_ambient_state", move |_args, _output| {
1025 crate::step_runtime::register_persona(
1026 "cancellation_entry",
1027 crate::step_runtime::PersonaDefinition {
1028 name: "cancel_persona".into(),
1029 stages: vec![crate::personas::StageDecl {
1030 name: "cancel_step".into(),
1031 allowed_tools: Some(vec!["cancel_tool".into()]),
1032 ..Default::default()
1033 }],
1034 ..Default::default()
1035 },
1036 );
1037 crate::step_runtime::register_step(
1038 "cancelled_step",
1039 crate::step_runtime::StepDefinition {
1040 name: "cancel_step".into(),
1041 function: "cancelled_step".into(),
1042 model: Some("cancel-model".into()),
1043 ..Default::default()
1044 },
1045 );
1046 assert!(crate::step_runtime::maybe_push_active_persona(
1047 "cancellation_entry",
1048 1
1049 ));
1050 assert!(crate::step_runtime::maybe_push_active_step(
1051 "cancelled_step",
1052 2,
1053 &[]
1054 ));
1055 assert_eq!(
1056 crate::stdlib::process::source_root_path(),
1057 imported_source_dir
1058 );
1059 assert_eq!(
1060 crate::step_runtime::current_persona_name().as_deref(),
1061 Some("cancel_persona")
1062 );
1063 assert_eq!(
1064 crate::step_runtime::active_step_model_default().as_deref(),
1065 Some("cancel-model")
1066 );
1067 assert_eq!(
1068 crate::orchestration::current_execution_policy()
1069 .unwrap()
1070 .tools,
1071 vec!["cancel_tool"]
1072 );
1073 crate::stdlib::process::set_thread_source_dir(&poisoned_source_dir);
1074 crate::stdlib::process::set_thread_execution_context(Some(
1075 crate::orchestration::RunExecutionRecord {
1076 adapter: Some("cancelled".into()),
1077 ..Default::default()
1078 },
1079 ));
1080 crate::orchestration::push_approval_policy(
1081 crate::orchestration::ToolApprovalPolicy {
1082 auto_deny: vec!["cancel_tool".into()],
1083 ..Default::default()
1084 },
1085 );
1086 if let Some(sender) = ambient_started_for_builtin.lock().unwrap().take() {
1087 let _ = sender.send(());
1088 }
1089 Ok(VmValue::Nil)
1090 });
1091
1092 std::fs::write(
1093 &imported_path,
1094 r#"
1095@persona(name: "cancel_persona", stages: [{name: "cancel_step", allowed_tools: ["cancel_tool"]}])
1096pub fn cancellation_entry(agent: HarnessAgent) {
1097 return cancelled_step(agent)
1098}
1099
1100@step(name: "cancel_step", model: "cancel-model")
1101fn cancelled_step(agent: HarnessAgent) {
1102 agent.pipeline_on_finish({ _h, value -> value })
1103 const child = spawn {
1104 child_started()
1105 wait_for_child_release()
1106 child_effect()
1107 }
1108 strand_ambient_state()
1109 wait_forever()
1110}
1111"#,
1112 )
1113 .unwrap();
1114 let mut helper_exports = vm
1115 .load_module_exports_from_source(
1116 "<cancellation-helper>",
1117 "pub fn outer_callback(_h, value) { return value }\n\
1118 pub fn answer() { return 42 }",
1119 )
1120 .await
1121 .unwrap();
1122 let callable = helper_exports.remove("answer").unwrap();
1123 let outer_callback = helper_exports.remove("outer_callback").unwrap();
1124 let slow = compile_harn(&format!(
1125 "import {{ cancellation_entry }} from \"{}\"\n\
1126 pipeline default(harness: Harness) {{ return cancellation_entry(harness.agent) }}",
1127 imported_path.display()
1128 ));
1129
1130 let baseline_execution = crate::orchestration::RunExecutionRecord {
1131 cwd: Some(baseline_dir.path().display().to_string()),
1132 source_dir: Some(baseline_dir.path().display().to_string()),
1133 adapter: Some("baseline".into()),
1134 ..Default::default()
1135 };
1136 let baseline_policy = crate::orchestration::CapabilityPolicy {
1137 tools: vec!["baseline_tool".into(), "cancel_tool".into()],
1138 ..Default::default()
1139 };
1140 let baseline_approval = crate::orchestration::ToolApprovalPolicy {
1141 auto_approve: vec!["baseline_tool".into()],
1142 ..Default::default()
1143 };
1144 crate::stdlib::process::set_thread_source_dir(baseline_dir.path());
1145 crate::stdlib::process::set_thread_execution_context(Some(
1146 baseline_execution.clone(),
1147 ));
1148 crate::orchestration::push_execution_policy(baseline_policy.clone());
1149 crate::orchestration::push_approval_policy(baseline_approval.clone());
1150 crate::orchestration::set_pipeline_on_finish(Arc::clone(&outer_callback));
1151 let outer_span =
1152 crate::tracing::span_start(crate::tracing::SpanKind::Pipeline, "outer".into());
1153
1154 let mut execution =
1155 Box::pin(vm.execute_with_timeout(&slow, Duration::from_secs(30)));
1156 tokio::select! {
1157 biased;
1158 result = &mut execution => panic!("slow execution unexpectedly finished: {result:?}"),
1159 started = async {
1160 ambient_started_rx.await.expect("step did not reach cancellation point");
1161 while !child_started.load(Ordering::Acquire) {
1162 tokio::task::yield_now().await;
1163 }
1164 } => started,
1165 }
1166 drop(execution);
1167
1168 assert!(!vm.execution_deadline.is_active());
1169 assert!(!vm.frames.is_empty(), "fixture must abandon a live frame");
1170 assert_eq!(
1171 crate::stdlib::process::source_root_path(),
1172 baseline_dir.path()
1173 );
1174 assert_eq!(
1175 crate::stdlib::process::current_execution_context(),
1176 Some(baseline_execution.clone())
1177 );
1178 assert_eq!(
1179 crate::orchestration::current_execution_policy(),
1180 Some(baseline_policy.clone())
1181 );
1182 assert_eq!(
1183 crate::orchestration::current_approval_policy(),
1184 Some(baseline_approval.clone())
1185 );
1186 assert!(crate::step_runtime::current_persona_name().is_none());
1187 assert!(crate::step_runtime::active_step_model_default().is_none());
1188 assert_eq!(crate::tracing::current_span_id(), Some(outer_span));
1189 let restored_callback =
1190 crate::orchestration::take_pipeline_on_finish().unwrap();
1191 assert!(Arc::ptr_eq(&restored_callback, &outer_callback));
1192 crate::orchestration::set_pipeline_on_finish(restored_callback);
1193 let abandoned_spans = crate::tracing::peek_spans();
1194 assert!(abandoned_spans.iter().any(|span| {
1195 span.name == "cancel_step"
1196 && span.metadata.get("status") == Some(&serde_json::json!("abandoned"))
1197 }));
1198 assert!(abandoned_spans.iter().any(|span| {
1199 span.name == "main"
1200 && span.metadata.get("status") == Some(&serde_json::json!("abandoned"))
1201 }));
1202
1203 let frame_depth = vm.frames.len();
1204 let output = vm.output().to_string();
1205 let error = vm.execute(&quick).await.unwrap_err();
1206 assert!(matches!(error, VmError::AbandonedExecution));
1207 let closure_error = vm.call_closure_pub(&callable, &[]).await.unwrap_err();
1208 assert!(matches!(closure_error, VmError::AbandonedExecution));
1209 let source_cache_len = vm.source_cache.len();
1210 let module_cache_len = vm.module_cache.len();
1211 let module_error = vm
1212 .load_module_exports_from_source(
1213 "<poisoned-module-load>",
1214 "pub fn poisoned() { return 0 }",
1215 )
1216 .await
1217 .unwrap_err();
1218 assert!(matches!(module_error, VmError::AbandonedExecution));
1219 assert_eq!(vm.source_cache.len(), source_cache_len);
1220 assert_eq!(vm.module_cache.len(), module_cache_len);
1221 let start_error = vm.start(&quick).unwrap_err();
1222 assert!(matches!(start_error, VmError::AbandonedExecution));
1223 let restart_error = vm.restart_frame(0).unwrap_err();
1224 assert!(matches!(restart_error, VmError::AbandonedExecution));
1225 assert_eq!(vm.frames.len(), frame_depth);
1226 assert_eq!(vm.output(), output);
1227
1228 let mut vm_b = Vm::new();
1229 register_vm_stdlib(&mut vm_b);
1230 let observed_callback = Arc::clone(&outer_callback);
1231 let observed_dir = baseline_dir.path().to_path_buf();
1232 vm_b.register_builtin("observe_baseline", move |_args, _output| {
1233 assert_eq!(crate::stdlib::process::source_root_path(), observed_dir);
1234 assert_eq!(
1235 crate::stdlib::process::current_execution_context(),
1236 Some(baseline_execution.clone())
1237 );
1238 assert_eq!(
1239 crate::orchestration::current_execution_policy(),
1240 Some(baseline_policy.clone())
1241 );
1242 assert_eq!(
1243 crate::orchestration::current_approval_policy(),
1244 Some(baseline_approval.clone())
1245 );
1246 assert!(crate::step_runtime::current_persona_name().is_none());
1247 assert!(crate::step_runtime::active_step_model_default().is_none());
1248 let callback = crate::orchestration::take_pipeline_on_finish().unwrap();
1249 assert!(Arc::ptr_eq(&callback, &observed_callback));
1250 crate::orchestration::set_pipeline_on_finish(callback);
1251 Ok(VmValue::Nil)
1252 });
1253 let vm_b_chunk =
1254 compile_harn("pipeline default(harness: Harness) { observe_baseline(); return 42 }");
1255 assert!(matches!(
1256 vm_b.execute(&vm_b_chunk).await.unwrap(),
1257 VmValue::Int(42)
1258 ));
1259 assert_eq!(crate::tracing::current_span_id(), Some(outer_span));
1260
1261 drop(vm);
1262 child_release.notify_one();
1263 for _ in 0..10 {
1264 tokio::task::yield_now().await;
1265 }
1266 assert!(
1267 !child_effect.load(Ordering::Acquire),
1268 "dropping an abandoned VM must abort spawned side effects"
1269 );
1270 crate::reset_thread_local_state();
1271 })
1272 .await;
1273 }
1274
1275 #[tokio::test(flavor = "current_thread")]
1276 async fn natural_host_deadline_is_terminal_not_abandoned() {
1277 let local = tokio::task::LocalSet::new();
1278 local
1279 .run_until(async {
1280 let infinite = compile_harn("pipeline default(harness: Harness) { while true {} }");
1281 let quick = compile_harn("pipeline default(harness: Harness) { return 42 }");
1282 let mut vm = Vm::new();
1283 register_vm_stdlib(&mut vm);
1284
1285 let error = vm
1286 .execute_with_timeout(&infinite, Duration::ZERO)
1287 .await
1288 .unwrap_err();
1289 assert!(matches!(error, VmError::ExecutionDeadlineExceeded));
1290 assert!(!vm.execution_deadline.is_abandoned());
1291 assert!(matches!(
1292 vm.execute(&quick).await.unwrap(),
1293 VmValue::Int(42)
1294 ));
1295 })
1296 .await;
1297 }
1298
1299 #[tokio::test(flavor = "current_thread")]
1300 async fn host_admission_extends_execution_deadline_by_injected_clock_delta() {
1301 let chunk = compile_harn(
1302 r"
1303pipeline default(harness: Harness) {
1304 wait_for_admission()
1305 return 42
1306}
1307",
1308 );
1309 let mut vm = Vm::new();
1310 register_vm_stdlib(&mut vm);
1311 let clock = harn_clock::PausedClock::new(time::OffsetDateTime::UNIX_EPOCH);
1312 let admission_clock: Arc<dyn harn_clock::Clock> = clock.clone();
1313 vm.register_async_builtin("wait_for_admission", move |ctx, _args| {
1314 let clock = Arc::clone(&clock);
1315 let admission_clock = Arc::clone(&admission_clock);
1316 async move {
1317 let before = ctx.execution_deadline_offset_for_test();
1318 let pause = ctx
1319 .pause_execution_deadline(admission_clock)
1320 .expect("timed execution exposes its outer deadline to inline host work");
1321 clock.advance(Duration::from_millis(25));
1322 drop(pause);
1323 let after = ctx.execution_deadline_offset_for_test();
1324 assert_eq!(after.saturating_sub(before), 25_000_000);
1325 Ok(VmValue::Nil)
1326 }
1327 });
1328
1329 let value = vm
1330 .execute_with_timeout(&chunk, Duration::from_secs(5))
1331 .await
1332 .expect("host admission extends rather than spends the execution budget");
1333 assert!(matches!(value, VmValue::Int(42)));
1334 }
1335
1336 #[tokio::test(flavor = "current_thread")]
1337 async fn timed_finite_loop_keeps_sync_opcodes_on_direct_dispatch() {
1338 let local = tokio::task::LocalSet::new();
1339 local
1340 .run_until(async {
1341 let chunk = compile_harn(
1342 r"
1343pipeline default(harness: Harness) {
1344 let total = 0
1345 for i in 0 to 10000 {
1346 total = total + i
1347 }
1348 return total
1349}
1350",
1351 );
1352 let mut vm = Vm::new();
1353 register_vm_stdlib(&mut vm);
1354 reset_scope_interrupt_async_dispatches();
1355
1356 let value = vm
1357 .execute_with_timeout(&chunk, Duration::from_secs(1))
1358 .await
1359 .unwrap();
1360
1361 assert!(matches!(value, VmValue::Int(_)));
1362 assert!(
1363 scope_interrupt_async_dispatches() <= 4,
1364 "finite sync loop fell back to per-op async dispatch"
1365 );
1366 })
1367 .await;
1368 }
1369}